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.
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.
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.
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.
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?
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...
}
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.
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.