6

jmap help shows:

...

-dump:<dump-options> to dump java heap in hprof binary format
                    dump-options:
                      live         dump only live objects; if not specified,
                                   all objects in the heap are dumped.

...

Once I dump a Tomcat (with java param -Xmx384m) heap:

jmap -dump:file=dump.bin <pid>

I got a dump file of ~300M.

When I dump its heap with live objects only:

jmap -dump:live,file=live-dump.bin <pid>

I got a dump file of ~120M.

My guess of live objects may be:

  1. Objects in young generation;

  2. Objects that are used / referenced / reachable and will not be collected.

Which one is right?

UPDATE

My guess #2 seems correct, and thanks for Alexey Ragozin's explanation (live option will cause a full GC). I tested again according to his hint:

jmap -dump:file=dump.hprof <pid>
jmap -dump:live,file=live-dump.hprof <pid>
jmap -dump:file=after-live-dump.hprof <pid>

size of these 3 files are:

dump.hprof ~190MB
live-dump.hprof ~40MB
after-live-dump.hprof ~40MB

so after -dump:live, almost all objects in heap are live.

auntyellow
  • 2,423
  • 2
  • 20
  • 47

1 Answers1

14

jmap -dump:live,file=live-dump.bin <pid>

live option in jmap command below forces JVM to do a full GC before dumping content of heap into a file.

After full GC only objects transitively reachable from GC roots (definition of "live") are remaining in heap.

Alexey Ragozin
  • 8,081
  • 1
  • 23
  • 23
  • When you use live option, full GC is performed by the JVM which could literally hang the application until its complete, in some cases takes a long time and it may not even finish for application that use large heaps. In such cases, there is a good chance of getting the heap dump done if you drop the live option – webjockey Jun 23 '22 at 13:00