This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JDialog memory leak with code example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog memory leak with code example" Watch "JDialog memory leak with code example" New topic
Author

JDialog memory leak with code example

Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
I posted previously that on my main window when the user clicks a button I open a dialog that displays a large amount of information in a table. When the user closes the dialog, the memory is never freed. After the user does this several times the JVM runs out of memory.

Someone posted that this was a Sun bug and that I should override the dispose method and remove all the components. I tried that but still have the problem.

I recreated the problem using a much smaller peice of test code (see below). If you run this code while watching the task manager (if using windows), you can see each time you open and close the dialog you lose a lot of memory. After 6 or 7 times the JVM will run out of memory.

Anybody have any ideas? Is there something simple I'm missing?

Here is the main window class...



Here is the dialog class that shows an array in a table...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Where did you post this previously? I would like to read that discussion.


GenRocket - Experts at Building Test Data
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
I posted here...

http://www.coderanch.com/t/343694/GUI/java/JDialog-memory-leak
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I just opened/closed the jialog 100 times - no problems.

the numbers in task manager changed very little.

win xp pro, 2gb ram, 1.5.0_05
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
I am running 1.5.0_08 and XP. I just verified the problem does not occur under 1.4.2_12.

I'm downloading 1.5.0_09 right now and I'll give that a shot.
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
I just download 1.5.0_09 and the problem still occurs. After just a few clicks of the button the JVM runs out of memory.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I don't see the dispose method in your example code.


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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Todd Johnson:



That code is definitely not necessary. The local variables will get out of scope anyway, and the garbage collector is guaranteed to be called before you get an OOME.
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
I know the code setting the references to NULL are not necessary, I just added it to be very clear the references were going away.

I set the default close operation to dispose on exit. I have also tried calling dispose explicity and it behaves the same. A few clicks and it throws an out of memory exception.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Todd, you can't do anything about the dialog object not being garbange collected, as far as I know. So what you should do is getting rid of as many resources in its dispose method as possible.



That should at least make all the objects garbage collectible that are used by the dialog.
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
Ilja,

I added the dispose() method that you proposed. I also added an explicit call to dispose() after the dialog closes.

It still runs the JVM out of memory after 4 - 6 open/closes of the dialog. It works fine under 1.4.2. Looks like a major 1.5.0_08 and 1.5.0_09 bug to me.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's strange. Sounds like you should use a profiler to find out what is taking up the memory, and what holds it from being gced...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Ilja Preuss:
That's strange. Sounds like you should use a profiler to find out what is taking up the memory, and what holds it from being gced...


Yea, it has something to do with the table. I reproduced the error on _09 but went into the dialog and removed all the table stuff. So it's just an empty JDialog now. Didn't have the problem again. Add the table back in, problem shows up.

So while it may be a bug in the VM (most likely) you'll still need to use something to determine how to fix it until Sun does.
Todd Johnson
Ranch Hand

Joined: Sep 03, 2005
Posts: 61
Thanks Gregg. When I get a chance I'll have to work on a work-around.

I'm amazed this isn't breaking tons of code.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Todd Johnson:
Thanks Gregg. When I get a chance I'll have to work on a work-around.

I'm amazed this isn't breaking tons of code.


I'm not that amazed. I don't think a lot of people use a JDialog the same way you are to display a lot of tabular data.
John Zli
Greenhorn

Joined: Feb 24, 2009
Posts: 1
Hi, Todd, I was scouting the web looking for answers of the similar question. In my case it was JTable. Did you find a work-around?

Thanks,

John
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

John, you may have missed it, but this thread was over 2 years old. In fact, Todd hasn't posted a single message for months now.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDialog memory leak with code example