Question:
Java memory leak problem?
HT-5
2009-03-09 17:02:27 UTC
I wrote a large ( >60 pages ) java program and used swing for a GUI and I was careful to close all of my readers, output streams, and dispose of all frames when the user is done with them. I also do System.gc() at the end of each class so all the garbage should be collected. However I still have a memory leak. About 20MB are leaked for every ten (large) classes the program goes through. How can I be leaking this much memory when I was so careful when writing the code?
Four answers:
jgoulden
2009-03-09 17:26:35 UTC
Well, Java doesn't guarantee that the garbage collector will ever be called during the program's lifetime even if you make explicite calls to System.gc() as you have. Typically, the garbage collector won't run unless the program needs more memory than it has. So, you may just have to live with your memory leak - if indeed it actually has one. The referenced article discusses this problem in some detail.
tinned_tuna
2009-03-09 17:18:44 UTC
The only real way to answer this is to thoroughly investigate the code for potential memory leaks.



Some common causes are things like leaving data in Arrays, Lists, ArrayLists or other collections, and not explicitly disposing of them, or dereferencing them. Instantiating objects with a very large amount of instance variables that are not eligible for garbage collection.



The other possibility is that your app is simply overly large.



Memory leaks are possible, even when disposing everything that the rule book says you're supposed to, because you could be using some thing that the rule book doesn't cover (This 'rule book' is hypothetical, by the way).



All I can say is thoroughly investigate all of your code, and possibly even give it to your friend to look over.
ginyard
2016-10-16 01:39:22 UTC
Sloppy code is in no way reliable code - and that is in no way uncomplicated to maintain. Spend the time to circulate back and close each and everything now, or you would be spending 5 cases as plenty time doing it later, once you have forgotten what you have been doing.
-Aoi-
2009-03-09 17:15:54 UTC
sorry bout im a novice programmer too... trying to learn java from the start... ive learned c++ and pissed off because it is not a garbage collecting languge... i hope this links help



http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html





my blog: http://jdsalingersknowjava.blogspot.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...