I've been doing web development in java for a while, but just recently started messing around with swing. So I hope this isn't a stupid question...
I have a main window. When the user clicks a buton I go out to the database and look a bunch of information up and store it in an array. I then instantiate a class (ResultsDialog) I created to display these results passing the array into the constructor. The ResultsDialog class I created to display the results extends the JDialog class.
The ResultsDialog class stores the array off in a private member variable. I have the dialog set as modal and DISPOSE_ON_CLOSE. When the user closes the dialog, the method in the main window class that created the dialog ends, it's the references to the ResultClass and the array go out of scope.
When the user clicks the button on the main window, the whole process repeats. I look up more data and store it in an array, create a new ResultClass, etc.
The problem I have is that the array is large, and for some reason the memory is not being freed after the dialog closes and it's reference goes out of scope. If I do this enough times, I run the JVM out of memory.
My guess is that somehow there is still a refence left between my main JFrame and the JDialog that is preventing it from being garbage collected. Is there something else that needs to be done after a JDialog closes to ensure that it can be garbage collected?
Swing has a nasty bug that can prevent dialogs and frames from being garbage collected even when they are disposed.
The "solution" is to override the dialogs dispose method so that it removes all components from itself and all references to other objects.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
posted 12 years ago
Thanks for the info guys!
I'm very suprised sun would let such a glaring and dangerous situation exist. I'm lucky I was dealing with JDialogs that used such a large amount of memory that it was obvious that I had a problem. Otherwise if I was only losing small amounts of memory it would have been much harder to track down the culprit.