概述

jinfo 是 JDK 自带的命令,可以用来查看正在运行的 java 应用程序的扩展参数,包括Java System属性和JVM命令行参数;也可以动态的修改正在运行的 JVM 一些参数。当系统崩溃时,jinfo可以从core文件里面知道崩溃的Java应用程序的配置信息。

用法

[root@centos7 ~]# jinfo -h
Usage:
    jinfo [option] <pid>
        (to connect to running process) #连接一个运行的进程ID
    jinfo [option] <executable <core>
        (to connect to a core file)
    jinfo [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server) #连接一个远程的server

where <option> is one of:
    -flag <name>         to print the value of the named VM flag
    -flag [+|-]<name>    to enable or disable the named VM flag
    -flag <name>=<value> to set the named VM flag to the given value
    -flags               to print VM flags
    -sysprops            to print Java system properties
    <no option>          to print both of the above
    -h | -help           to print this help message

实战

查看jvm的非默认的参数值

[root@centos7 ~]# jinfo -flags 11968
Attaching to process ID 11968, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.172-b11
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=31457280 -XX:MaxHeapSize=482344960 -XX:MaxNewSize=160432128 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=10485760 -XX:OldSize=20971520 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
#内存单位为字节

查看jvm系统详细参数及环境变量

[root@centos7 ~]# jinfo 11968
Attaching to process ID 11968, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.172-b11
Java System Properties:

java.io.tmpdir = /tmp
java.vm.specification.vendor = Oracle Corporation
os.name = Linux
sun.jnu.encoding = UTF-8
java.library.path = /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
spring.beaninfo.ignore = true
java.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 3.10.0-1062.el7.x86_64
user.home = /root
user.timezone = Asia/Shanghai
catalina.useNaming = false
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
catalina.home = /opt/front/work
user.name = root
java.class.path = /opt/rooster/rooster-demo.war
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = /opt/rooster/rooster-demo.war
java.home = /opt/app/jdk1.8.0_172/jre
user.language = zh
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.8.0_172
java.ext.dirs = /opt/app/jdk1.8.0_172/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /opt/app/jdk1.8.0_172/jre/lib/resources.jar:/opt/app/jdk1.8.0_172/jre/lib/rt.jar:/opt/app/jdk1.8.0_172/jre/lib/sunrsasign.jar:/opt/app/jdk1.8.0_172/jre/lib/jsse.jar:/opt/app/jdk1.8.0_172/jre/lib/jce.jar:/opt/app/jdk1.8.0_172/jre/lib/charsets.jar:/opt/app/jdk1.8.0_172/jre/lib/jfr.jar:/opt/app/jdk1.8.0_172/jre/classes
java.awt.headless = true
java.vendor = Oracle Corporation
catalina.base = /opt/front/work
file.separator = /
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.cpu.isalist = 

VM Flags:
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=31457280 -XX:MaxHeapSize=482344960 -XX:MaxNewSize=160432128 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=10485760 -XX:OldSize=20971520 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
Command line:  

查看最大堆内存

[root@centos7 ~]# jinfo -flag MaxHeapSize 11968
-XX:MaxHeapSize=482344960

开启dump文件转储

[root@centos7 ~]# jinfo -flag +HeapDumpOnOutOfMemoryError 11968
[root@centos7 ~]# jinfo -flags 11968
Attaching to process ID 11968, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.172-b11
Non-default VM flags: -XX:CICompilerCount=2 -XX:+HeapDumpOnOutOfMemoryError -XX:InitialHeapSize=31457280 -XX:MaxHeapSize=482344960 -XX:MaxNewSize=160432128 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=10485760 -XX:OldSize=20971520 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
Command line:  

修改参数的值

虽然jinfo支持动态修改参数,但不是所有参数都支持修改,如修改最大堆内存就会异常

[root@centos7 ~]# jinfo -flag MaxHeapSize=268435456 11968
Exception in thread "main" com.sun.tools.attach.AttachOperationFailedException: flag 'MaxHeapSize' cannot be changed

        at sun.tools.attach.LinuxVirtualMachine.execute(LinuxVirtualMachine.java:229)
        at sun.tools.attach.HotSpotVirtualMachine.executeCommand(HotSpotVirtualMachine.java:261)
        at sun.tools.attach.HotSpotVirtualMachine.setFlag(HotSpotVirtualMachine.java:234)
        at sun.tools.jinfo.JInfo.flag(JInfo.java:134)
        at sun.tools.jinfo.JInfo.main(JInfo.java:81)

设置dump文件位置

[root@centos7 rooster]# jinfo -flag HeapDumpPath=/opt/rooster 11968
[root@centos7 rooster]# jinfo -flag HeapDumpPath 11968
-XX:HeapDumpPath=/opt/rooster

发表评论

电子邮件地址不会被公开。 必填项已用*标注