• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDialog memory leak with code example

 
Todd Johnson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you post this previously? I would like to read that discussion.
 
Todd Johnson
Ranch Hand
Posts: 61
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the dispose method in your example code.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic