File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDialog and memory leak

 
Todd Johnson
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid these kind of things I usually try to keep a single dialog and reuse it as needed.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Todd Johnson
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic