views:

114

answers:

6

I have a dialog. Every time I create it and then dispose, it stays in memory.

It seems to be a memory leak somewhere, but I can't figure it out. Do you have any ideas? See the screenshot of heap dump for more information.

Thanks in advance.

http://img441.imageshack.us/img441/5764/leak.png

A: 

Are you sure that you dispose your dialog? The standard of JDialog is HIDE_ON_CLOSE. Changing the defaultCloseOperation to DISPOSE_ON_CLOSE could help if you haven't done so already.

Ham
Yes. I'm sure I use DISPOSE_ON_CLOSE as a default operation.
Pavel
+1  A: 

If I understand your screenshot correctly, all the referents are weak, so eventually it will get GCd. But there are no guarantees about when it will GCd; typically it won't happen immediately. You can try to hurry up GC by running the garbage collector (System.gc()), but there are still no guarantees whether it will collect all the garbage or not.

Joonas Pulakka
I think I waited enough. Nothing changes during a long time. Also I enforced GC to collect by invocation of System.gc() :(
Pavel
Note that even System.gc() is just a "suggestion"; while it does run the garbage collector, the collector still does what it likes. There's no way to enforce it to collect everything that could, in principle, be collected. One thing to try is to use lots of memory for something else; maybe it will make the collector a bit more active in deleting unnecessary stuff.
Joonas Pulakka
A: 

As long as you hold a reference to this window, it will not be removed by garbage-collection. You are sure you not only dispose it (that would only free resources associated with the window, not the window itself), but also clear all references?

Mnementh
I clear all references inside window when I dispose the window.
Pavel
Then what does your question mean? You are asking why GC doesn't clear the Dialog references. What references?
EJP
The memory that was allocated for dialog remains in use. I asked why, if there are no references on the object.
Pavel
Did the next Garbage collection run to reclaim the memory? Until then the memory is not freed.
Mnementh
Here is the graphs of GC activity and the heap size. A hundred of dialogs were created and disposed at the first 5 minutes. After next ten minutes they were still in the memory. http://img12.imageshack.us/img12/8555/32464004.png
Pavel
A: 

Depending in which generation of the heap your object is it will or will not be garbage collected by a minor GC. To be sure that all non-strong-referenced objects are GCed you have to run a full GC, and the way to achieve this is to provoke an OutOfMemoryError... You could do it with the following code:

try {
        List<byte[]> list = new ArrayList<byte[]>();
        while (true) {
            list.add(new byte[1024]);
        }
    } catch (OutOfMemoryError oome) {
        // full GC should have run here...
    }
pgras
A: 

Are you sure a GC has actually been run? Start your app with java -verbose:gc etc and the VM will dump a status report to the console every time a GC is performed. More details on garbage collection tuning for Java 6.

gustafc
A: 

The others are right about System.gc() being a suggestion. As an alternative, you might examine your program's heap with a tool that implements the JVM Tool Interface method ForceGarbageCollection, such as the NetBeans profiler.

trashgod