1

I am working on a Swing application and recently, I started to see the following problem:

I have to display in a separate window a very large job report. I close this window, then I re-open the same job report and I get an OutOfMemory Java heap space error.

The JVM is started with -Xmx512m and all the objects that are created when I open the job report take about 300MB on heap. Assuming that there isn't a memory leak, I would expect that the second time I open the same job report the JVM won't throw the OOM. But, looking at the GC logs after closing the first window, I don't see any GC activity.

The strange thing is that after I close the first window, if I take a heap dump with jmap (without the "live" option) the objects can still be seen in the heap dump.

If I run jmap with the dump:live option, the followings happen:

  • after the first heap dump is taken, I can still see the objects on the heap.
  • when I take the second heap dump, it no longer contains those objects, I can re-open the same job report again, with no problems. So, if it was a memory leak, then those objects could not have been collected, am I right?

I tested this on Java 6 (1.6.0_25 ad 1.6.0_45, on Windows) and it reproduces all the time.

Running jmap -heap prints :

"using thread-local object allocation.
Mark Sweep Compact GC

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 536870912 (512.0MB)
   NewSize          = 1048576 (1.0MB)
   MaxNewSize       = 4294901760 (4095.9375MB)
   OldSize          = 4194304 (4.0MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 12582912 (12.0MB)
   MaxPermSize      = 134217728 (128.0MB)

"

JVM is started with the following options:

"   -Xms128m
    -Xmx512m
    -XX:MaxPermSize=128M
    -verbose:gc
    -XX:+PrintGCTimeStamps
    -XX:+PrintGCDetails
    -Xloggc:c:\my_gc.log
    -XX:+HeapDumpOnOutOfMemoryError"

So, my question is: why are all the objects collected when I take the heap dump with the live option (a sign that there isn't a memory leak) but, if I don't do this, I am unable to re-open another(or the same) job report because I get an OOM error?

Also, I tested another scenario:

  • open the first job report window, then closed it.
  • created a menu item that when pressed, creates in an infinite loop Long instances until the JVM throws an OOM. I inspected the heap dump that was generated when the OOM was thrown and the heap was filled 99% with Long instances and none of my job report objects were on the heap.

Thanks in advance.

trincot
  • 317,000
  • 35
  • 244
  • 286
silenoz81
  • 11
  • 2

1 Answers1

1

So, my question is: why are all the objects collected when I take the heap dump with the live option

The jmap live option forces a collection. It has been discussed here .

Your observations corroborate that.

What can be done as an exercise is re-opening the window post some GC activity to see whether the memory has been reclaimed.

Community
  • 1
  • 1
Ajay George
  • 11,759
  • 1
  • 40
  • 48