java 执行 jmap 时 JVM 是否停止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5287010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is a JVM stopped while executing jmap?
提问by StefanoS
Does my java application continue running while jmap is taking its memory dump?
当 jmap 进行内存转储时,我的 java 应用程序是否继续运行?
采纳答案by Stephen C
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.
这是“短暂”暂停还是“长时间”暂停取决于倾倒了多少。如果您使用“-dump”,那么您将转储整个堆,包括无法访问的对象。如果您使用“-dump:live”,您将只转储可访问的对象……但这也需要(至少)标记堆以找出哪些对象是可访问的。
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:
关于您可以通过使用 fork 避免停止 JVM 的建议,事实证明 fork 多线程进程可能有问题:
- fork in multi-threaded program
- Multithreaded fork
- http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them
Then there is is the resource usage issue.
然后是资源使用问题。
回答by Simon B
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.
我在尝试在生产机器上使用 jmap 创建 hprof 文件时遇到问题,并且很自然地将 java webapp 锁定了很长时间。
I found this page:
我找到了这个页面:
http://blogs.atlassian.com/2013/03/so-you-want-your-jvms-heap/
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.
这解释了您还可以使用 gdb(在 linux 系统上)来转储 java 进程的核心。
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.
有了这个核心文件,您就可以在单独的进程中生成 hprof 文件进行分析,以防止您的 java 服务器进程被中断这么长时间。如果您要使用 jmap 运行相同的操作,将会发生什么。
To summarise:
总结一下:
download and install gdb
下载并安装 gdb
apt-get update
apt-get install gdb
apt-get 更新
apt-get 安装 gdb
...
...
get the java process id of the java process you're interested in
获取您感兴趣的 java 进程的 java 进程 id
jps ...
jps ...
start a gdb session with that process
使用该进程启动 gdb 会话
gdb [pid] ...
gdb [pid] ...
then generate the core file:
然后生成核心文件:
gcore /tmp/jvm.core
gcore /tmp/jvm.core
end the gdb session
结束 gdb 会话
detach quit
分离退出
then use the core file generated to make an hprof file:
然后使用生成的核心文件制作一个 hprof 文件:
sudo jmap -dump:format=b,file=jvm.hprof /usr/bin/java /tmp/jvm.core
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.
然后 (g) 压缩文件并将其复制到您的机器上以供进一步分析。
回答by npellow
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.
我会说您的程序将在进行内存转储时短暂暂停。内存转储是您正在运行的程序的快照,因此在读取该内存时,jmap 需要暂时锁定 JVM。然而,要将转储文件发送回客户端,可以在单独的线程中完成,从而最大限度地减少暂停。