如何获取Java进程的最大堆大小?

3

我想检查我的应用程序是否正确获取了-Xmx选项。

Java进程在Docker容器中运行。

为了实现这一点,我需要找到创建的进程的最大堆大小。

起初,我尝试使用jmap命令,但出现了错误:

bash-4.2$ jmap -heap 128
Attaching to process ID 128, please wait...
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 128: Operation not permitted
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 128: Operation not permitted
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.execute(LinuxDebuggerLocal.java:163)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:278)
        at sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:671)
        at sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:611)
        at sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:337)
        at sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:304)
        at sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:140)
        at sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
        at sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
        at sun.jvm.hotspot.tools.HeapSummary.main(HeapSummary.java:49)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.tools.jmap.JMap.runTool(JMap.java:201)
        at sun.tools.jmap.JMap.main(JMap.java:130)
Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 128: Operation not permitted
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.access$100(LinuxDebuggerLocal.java:62)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$1AttachTask.doit(LinuxDebuggerLocal.java:269)
        at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.run(LinuxDebuggerLocal.java:138)

我不知道为什么 jmap 命令会失败。我搜索了相关的问题解决方案,但它们都没有帮助到我。

  • 我尝试过:
    • 使用创建进程的用户运行命令
    • 在 docker 容器外运行命令 docker exec container_id jcmd PID GC.heap_dump /tmp/docker.hprof

我不能修改现有的 Java 代码,所以我需要使用 CLI 来解决这个问题。

jstat 命令可以正常工作,那么有没有办法用 jstat 命令检查 jvm 的最大堆大小?

假设 (NGCMX+OGCMX) = (-Xmx value) 是否可行?

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html


3
错误提示为“不允许”,你尝试过这个吗?https://dev59.com/MVgR5IYBdhLWcg3wjd4F - Cyrille Con Morales
1
JMX 是一个选择吗? - Abra
1个回答

2
您可以使用jcmd工具:
 jcmd <you_vm_process_id> VM.flags | grep MaxHeapSize

这将显示最大堆大小(即使您没有自行设置)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接