为什么一些 jcmd 命令(例如:VM.set_flag)不能在所有 pid 上使用?

huangapple go评论80阅读模式
英文:

Why can some jcmd commands(eg: VM.set_flag) can not be used on all pid?

问题

当我想要修改 HeapDumpAfterFullGC 标志时,我使用 jps 查找我的程序的进程号(pid)。

然后我执行 jcmd 41032 help

41032:
以下命令可用:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

有关特定命令的更多信息,请使用 'help <command>'

VM.set_flags 未列出,而 jcmd 0 help 则有:

5957:
以下命令可用:
Compiler.CodeHeap_Analytics
Compiler.codecache
Compiler.codelist
Compiler.directives_add
Compiler.directives_clear
Compiler.directives_print
Compiler.directives_remove
Compiler.queue
GC.class_histogram
GC.class_stats
GC.finalizer_info
GC.heap_dump
GC.heap_info
GC.run
GC.run_finalization
JFR.check
JFR.configure
JFR.dump
JFR.start
JFR.stop
JVMTI.agent_load
JVMTI.data_dump
ManagementAgent.start
ManagementAgent.start_local
ManagementAgent.status
ManagementAgent.stop
Thread.print
VM.class_hierarchy
VM.classloader_stats
VM.classloaders
VM.command_line
VM.dynlibs
VM.flags
VM.info
VM.log
VM.metaspace
VM.native_memory
VM.print_touched_methods
**VM.set_flag**     # 这是我需要的
VM.stringtable
VM.symboltable
VM.system_properties
VM.systemdictionary
VM.uptime
VM.version
help

有关特定命令的更多信息,请使用 'help <command>'

我想知道为什么会出现这种情况,以及如何使用 jcmd 来更改 JVM 的标志。

英文:

When I want to change the HeapDumpAfterFullGC flag, I use jps to find the pid of my program.

41123 Jps
5957
41031 Launcher
41032 Main # this is mine

Then I execute jcmd 41032 help

41032:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use &#39;help &lt;command&gt;&#39;.

The VM.set_flags is not listed and jcmd 0 help has:


5957:
The following commands are available:
Compiler.CodeHeap_Analytics
Compiler.codecache
Compiler.codelist
Compiler.directives_add
Compiler.directives_clear
Compiler.directives_print
Compiler.directives_remove
Compiler.queue
GC.class_histogram
GC.class_stats
GC.finalizer_info
GC.heap_dump
GC.heap_info
GC.run
GC.run_finalization
JFR.check
JFR.configure
JFR.dump
JFR.start
JFR.stop
JVMTI.agent_load
JVMTI.data_dump
ManagementAgent.start
ManagementAgent.start_local
ManagementAgent.status
ManagementAgent.stop
Thread.print
VM.class_hierarchy
VM.classloader_stats
VM.classloaders
VM.command_line
VM.dynlibs
VM.flags
VM.info
VM.log
VM.metaspace
VM.native_memory
VM.print_touched_methods
**VM.set_flag**     # this have it
VM.stringtable
VM.symboltable
VM.system_properties
VM.systemdictionary
VM.uptime
VM.version
help

For more information about a specific command use &#39;help &lt;command&gt;&#39;.
41031:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use &#39;help &lt;command&gt;&#39;.
41032:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use &#39;help &lt;command&gt;&#39;.

I want to know why this happened and how I can use the jcmd to change the flag of JVM.

答案1

得分: 2

以下是翻译好的内容:

可用的jcmd命令列表取决于目标JVM的版本。

VM.set_flag命令首次出现在JDK 9中,具体详情请参见此链接

然而,HeapDumpAfterFullGC在JDK 8中也是一个可管理的选项,具体信息请参见此链接。这意味着即使jcmd没有列出该标志,也可以在运行时更改此标志。最简单的方法是使用jattach实用工具:

$ jattach <pid> setflag HeapDumpAfterFullGC 1

另一种设置标志的方法是使用JMX,例如通过jconsole。找到com.sun.management:type=HotSpotDiagnostic MXBean,并调用setVMOption操作:

为什么一些 jcmd 命令(例如:VM.set_flag)不能在所有 pid 上使用?

英文:

The list of available jcmd commands depends on the version of the target JVM.

VM.set_flag command first appeared in JDK 9.

However, HeapDumpAfterFullGC is a manageable option in JDK 8, too. This means, it is possible to change the flag in runtime, even though it is not listed by jcmd. The easiest way to do this is to use jattach utility:

$ jattach &lt;pid&gt; setflag HeapDumpAfterFullGC 1

Another way to set the flag is JMX: for example, with jconsole.
Find com.sun.management:type=HotSpotDiagnostic MXBean and invoke setVMOption operation:

为什么一些 jcmd 命令(例如:VM.set_flag)不能在所有 pid 上使用?

答案2

得分: 1

jcmd命令列出目标JVM支持的命令。这取决于目标JVM支持和实现了哪些命令。较新的JVM支持的命令比较老的JVM更多。

例如,在我的机器上,JDK 8的JVM接受大约20个这些命令:

2237216: (JDK 8 JVM)
以下命令可用:
VM.native_memory
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
VM.classloader_stats
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.uptime
VM.dynlibs
VM.flags
VM.system_properties
VM.command_line
VM.version

而JDK 14的JVM接受的命令更多,大约46个:

2218174:
以下命令可用:
Compiler.CodeHeap_Analytics
Compiler.codecache
Compiler.codelist
Compiler.directives_add
Compiler.directives_clear
Compiler.directives_print
Compiler.directives_remove
Compiler.queue
GC.class_histogram
GC.class_stats
GC.finalizer_info
GC.heap_dump
GC.heap_info
GC.run
GC.run_finalization
JFR.check
JFR.configure
JFR.dump
JFR.start
JFR.stop
JVMTI.agent_load
JVMTI.data_dump
ManagementAgent.start
ManagementAgent.start_local
ManagementAgent.status
ManagementAgent.stop
Thread.print
VM.class_hierarchy
VM.classloader_stats
VM.classloaders
VM.command_line
VM.dynlibs
VM.events
VM.flags
VM.info
VM.log
VM.metaspace
VM.native_memory
VM.print_touched_methods
VM.set_flag
VM.stringtable
VM.symboltable
VM.system_properties
VM.systemdictionary
VM.uptime
VM.version
help

因此,总之,你没有VM.set_flags,因为你正在运行的目标JVM不支持它。

另外,我不确定VM.set_flags会有什么确切的作用。正如@paulsm4提到的,我很确定事后改变HeapDumpAfterFullGC标志不会改变实际行为。我更倾向于搜索是否有直接的标志或JMX选项来实现这一点。

英文:

The jcmd command lists the commands the target JVM support.
It depends on the target JVM what commands it does support and implement.
Newer JVMs do support more commands than older JVMs.

For example, on my machine a JDK 8 JVM accepts these commands, about 20:

2237216: (JDK 8 JVM)
The following commands are available:
VM.native_memory
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
VM.classloader_stats
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.uptime
VM.dynlibs
VM.flags
VM.system_properties
VM.command_line
VM.version

And a JDK 14 JVM accepts way more commands, about 46:

2218174:
The following commands are available:
Compiler.CodeHeap_Analytics
Compiler.codecache
Compiler.codelist
Compiler.directives_add
Compiler.directives_clear
Compiler.directives_print
Compiler.directives_remove
Compiler.queue
GC.class_histogram
GC.class_stats
GC.finalizer_info
GC.heap_dump
GC.heap_info
GC.run
GC.run_finalization
JFR.check
JFR.configure
JFR.dump
JFR.start
JFR.stop
JVMTI.agent_load
JVMTI.data_dump
ManagementAgent.start
ManagementAgent.start_local
ManagementAgent.status
ManagementAgent.stop
Thread.print
VM.class_hierarchy
VM.classloader_stats
VM.classloaders
VM.command_line
VM.dynlibs
VM.events
VM.flags
VM.info
VM.log
VM.metaspace
VM.native_memory
VM.print_touched_methods
VM.set_flag
VM.stringtable
VM.symboltable
VM.system_properties
VM.systemdictionary
VM.uptime
VM.version
help

So, in summary. You don't have VM.set_flags, because your running target JVM doesn't support it.

Also, I'm not sure what VM.set_flags will exactly do. As @paulsm4 mentioned, I'm pretty sure changing the 'HeapDumpAfterFullGC' flag after the fact won't change the actual behavior. I would rather search if there is direct flag or JMX option for it.

huangapple
  • 本文由 发表于 2020年10月21日 15:36:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定