Is a JVM stopped while executing jmap?
Asked Answered
P

3

20

Does my java application continue running while jmap is taking its memory dump?

Petrography answered 13/3, 2011 at 2:32 Comment(0)
G
14

Your application is stopped. The only practical way to get an accurate heap dump would be to stop all application activity while the dump is being created.

Whether this is a "brief" pause or a "long" pause depends on how much is dumped. If you use "-dump" then you will dump the entire heap, including unreachable objects. If you use "-dump:live" you will only dump reachable objects ... but that also entails (at least) marking the heap to figure out which objects are reachable.

But if you are dumping a gigabyte sized heap, expect the pause time to be measured in minutes rather than seconds.


Re the suggestion that you could avoid stopping the JVM by using fork, it turns out that forking a multi-threaded process can be problematic:

Then there is is the resource usage issue.

Gauntry answered 13/3, 2011 at 2:45 Comment(6)
Even if this is how most/all JVM's do it today, I'm not sure this is the "only way to get an accurate heap dump". You could also fork and perform the heap dump from for child process. This would obviously use a lot of memory for the copy-on-write but probably not as much as 2x if you free pages as you go.Phenomenalism
@Ramon. Changed "only way" to "only practical way".Gauntry
@StephenC Would you expect the dump take minutes or even more for gigabyte+ sized heaps? It seems like it is growing the heap file very slowly and it is taking quite a long time.Induction
@Induction - I would expect it to be relatively fast. However, a slow file system or not having enough physical memory (compared with the JVM's vmem size) could lead to long dump times.Gauntry
I have a 7 GiB dump started 20 minutes ago where jmap is still running... This is on Windows Server 2012 R2.Megaera
Windows file systems have a reputation of being slow compared with Linux. And if you are dumping to a Windows share ....Gauntry
G
16

I had an issue with trying to do this on a production machine creating the hprof file with jmap took ages and naturally locked up the java webapp for ages.

I found this page:

http://blogs.atlassian.com/2013/03/so-you-want-your-jvms-heap/

Which explained that you can also use gdb (on linux systems) to dump the core of the java process.

With this core file you can then generate the hprof file for analysis in a separate process which prevent your java server process from being interrupted for such a long time. Which is what would happen if you were to run the same operation with jmap.

To summarise:

download and install gdb

apt-get update

apt-get install gdb

...

get the java process id of the java process you're interested in

jps ...

start a gdb session with that process

gdb [pid] ...

then generate the core file:

gcore /tmp/jvm.core

end the gdb session

detach quit

then use the core file generated to make an hprof file:

sudo jmap -dump:format=b,file=jvm.hprof /usr/bin/java /tmp/jvm.core

then (g)zip the file up and copy it to your machine for further analysis.

Gabfest answered 22/5, 2016 at 14:4 Comment(1)
WayBackMachine snapshot of original article web.archive.org/web/20200810143815/https://www.atlassian.com/…Scholiast
G
14

Your application is stopped. The only practical way to get an accurate heap dump would be to stop all application activity while the dump is being created.

Whether this is a "brief" pause or a "long" pause depends on how much is dumped. If you use "-dump" then you will dump the entire heap, including unreachable objects. If you use "-dump:live" you will only dump reachable objects ... but that also entails (at least) marking the heap to figure out which objects are reachable.

But if you are dumping a gigabyte sized heap, expect the pause time to be measured in minutes rather than seconds.


Re the suggestion that you could avoid stopping the JVM by using fork, it turns out that forking a multi-threaded process can be problematic:

Then there is is the resource usage issue.

Gauntry answered 13/3, 2011 at 2:45 Comment(6)
Even if this is how most/all JVM's do it today, I'm not sure this is the "only way to get an accurate heap dump". You could also fork and perform the heap dump from for child process. This would obviously use a lot of memory for the copy-on-write but probably not as much as 2x if you free pages as you go.Phenomenalism
@Ramon. Changed "only way" to "only practical way".Gauntry
@StephenC Would you expect the dump take minutes or even more for gigabyte+ sized heaps? It seems like it is growing the heap file very slowly and it is taking quite a long time.Induction
@Induction - I would expect it to be relatively fast. However, a slow file system or not having enough physical memory (compared with the JVM's vmem size) could lead to long dump times.Gauntry
I have a 7 GiB dump started 20 minutes ago where jmap is still running... This is on Windows Server 2012 R2.Megaera
Windows file systems have a reputation of being slow compared with Linux. And if you are dumping to a Windows share ....Gauntry
M
4

I would say your program will pause briefly while the memory dump is taken. The memory dump is a snapshot in time of your running program, so jmap will need to lock the JVM briefly while that memory is read. To send the dump file back to the client however, could be done in a separate thread, thereby minimizing the pause.

Mikkimiko answered 13/3, 2011 at 2:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.