File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JDialog and memory leak Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog and memory leak" Watch "JDialog and memory leak" New topic

JDialog and memory leak

Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
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?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
To avoid these kind of things I usually try to keep a single dialog and reuse it as needed.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
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
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
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.

Does the problem still exist in j2se 6?
I agree. Here's the link:
subject: JDialog and memory leak
It's not a secret anymore!