Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

JDialog and memory leak  RSS feed

 
Ranch Hand
Posts: 61
  • 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?
 
Ranch Hand
Posts: 1535
  • 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.
 
author
Sheriff
Posts: 14112
  • 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
  • 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
Boost this thread!