jinfo命令详解
jinfo
jinfo全称Java Configuration Info,所以它的主要作用是实时查看和调整JVM配置参数,
查看JVM参数
用法:jinfo -flag <name> PID
例如:
jinfo -flag MaxMetaspaceSize 18348,得到结果-XX:MaxMetaspaceSize=536870912,即MaxMetaspaceSize为512M
jinfo -flag ThreadStackSize 18348,得到结果-XX:ThreadStackSize=256,即Xss为256K
调整JVM参数
用法:
如果是布尔类型的JVM参数: jinfo -flag [+|-]<name> PID,enable or disable the named VM flag
如果是数字/字符串类型的JVM参数 jinfo -flag <name>=<value> PID,to set the named VM flag to the given value
那么哪些参数可以通过jinfo实时调整呢,请往下看;
查看所有支持动态修改的JVM参数
如何查看JVM支持的所有可以通过info动态修改的JVM参数参数呢?在官方文档中有一段这样的描述:
Flags marked as manageable are dynamically writeable through the JDK management interface (com.sun.management.HotSpotDiagnosticMXBean API) and also through JConsole.
也就是说,标记为manageable的参数或者通过com.sun.management.HotSpotDiagnosticMXBean这个类的接口得到;
方法一 -- 标记为manageable的Flags
Linux环境:java -XX:+PrintFlagsInitial | grep manageable
Window环境:java -XX:+PrintFlagsInitial | findstr manageable
运行结果如下图所示:
方法二 -- 通过HotSpotDiagnosticMXBean API
查看该类的 Java Doc,有一个getDiagnosticOptions()这样的方法,该方法的详细描述如下:
Returns a list of VMOption objects for all diagnostic options. A diagnostic option is a writeable VM option that can be set dynamically mainly for troubleshooting and diagnosis.
Bingo找到了,接下来通过Java代码调用该API得到所有可动态修改的JVM参数, java代码如下:
/**
* @author afei
* @version 1.0.0
* @since 2017年07月25日
*/
public class DiagnosticOptionsTest {
public static void main(String[] args) {
HotSpotDiagnostic mxBean = new HotSpotDiagnostic();
List<VMOption> diagnosticVMOptions = mxBean.getDiagnosticOptions();
for (VMOption vmOption:diagnosticVMOptions){
System.out.println(vmOption.getName() + " = " + vmOption.getValue());
}
}
}
说明:
由于HotSpotDiagnosticMXBean 是接口(public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean ),所以需要通过它唯一的实现类HotSpotDiagnostic 来调用这个方法;
运行结果如下:
HeapDumpBeforeFullGC = false
HeapDumpAfterFullGC = false
HeapDumpOnOutOfMemoryError = false
HeapDumpPath =
CMSAbortablePrecleanWaitMillis = 100
CMSWaitDuration = 2000
CMSTriggerInterval = -1
PrintGC = false
PrintGCDetails = false
PrintGCDateStamps = false
PrintGCTimeStamps = false
PrintGCID = false
PrintClassHistogramBeforeFullGC = false
PrintClassHistogramAfterFullGC = false
PrintClassHistogram = false
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
PrintConcurrentLocks = false
UnlockCommercialFeatures = false
这些JVM参数就是所有可以通过jinfo动态修改的参数;