My program keeps giving an OutOfMemoryError. I was wondering if there were any tips to find out where the memory is going. I can't seem find a pattern to when it gives up the ghost. I have put a Static count variable into each of my classes which increments in the constructor and takes off one in the finalize method and prints out the count. This seems to be behaving as I expected ie. there is the correct number of each of my objects.
you can use profilers to find memory leak in your application (if you cannot figure it out by logging or tracing the code execution)
there are plenty of profilers available to java developers. one of them is Netbeans profiler (if you use netbeans you can use this profiler) im sure that thre are profilers for eclipse like jProiler and yourkit java profiler.
A couple easy ways to accidentally chew up all memory are recursive routines that forget to bottom out and return, and loops that forget to stop and keep creating objects. If your out of memory problems occur suddenly at startup or right after kicking off some operation, you might look for those situations.
With JDK 5 and later you get JConsole which will let you watch memory use and object instance counts. Those might give you a clue how fast the problem comes on and when it starts.
Of course it may be you don't have a coding bug at all, but just use a lot of memory. If that's the case you may be able to bump up the heap to handle it. Let us know what you find!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Thanks everyone. I have tried some profilers and now I'm REALLY confused! I've tried JConsole and NetBeans. I'm a bit overwhelmed with the info from NetBeans, but it seems there are a lot of int from BufferedImage. My program is a sort of flower hybidising thing. I have a singleton class called FlowerSimState which holds all the information, and the panels of the gui are registered as observers. What's really confusing me is that if I press the button with the OffspringListener repeatedly (50 times), the memory usage constantly increases untill it dies. If I press the RandomListener button the memory fluctuates, but it calls the same function as the OffspringListener! If I press other buttons, the OffspringListener still kills the program but at a less predicatable rate.
I've included some code, but it's not all there as I tried to simplify to make it more readable. I only realise how confusing my code is when I try to explain it to someone else.