本文使用jinfo命令工具~
jinfo用于查看运行中的java程序的参数,同时也支持在运行时修改部分参数~ 参数包括Java System属性和JVM命令行参数。
在使用jinfo命令之前,可以先使用jps -help来查看jps命令相关的使用方法,包括有哪些参数等~
[root@dev18 ~]# jinfo -help
Usage:
jinfo [option] <pid>
(to connect to running process)
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)
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
[root@dev18 ~]#
jinfo 需要pid,所以需要和jps配合使用,也就是先使用jps获取pid信息,然后使用jinfo来处理~
[root@dev18 ~]# jps -l
1656 org.elasticsearch.bootstrap.Elasticsearch
18011 sun.tools.jps.Jps
12905 org.apache.zookeeper.server.quorum.QuorumPeerMain
4308 /srv/activemq/apache-activemq-5.14.3//bin/activemq.jar
[root@dev18 ~]#
接下来,本文使用Zookeeper的服务为例,其pid为12905
使用jinfo -flags 12905打印JVM flags信息
[root@dev18 ~]# jinfo -flags 12905
Attaching to process ID 12905, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.71-b01
-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false
[root@dev18 ~]#
使用jinfo -flags 12905打印JVM flags信息
[root@dev18 ~]# jinfo -flags 12905
Attaching to process ID 12905, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.71-b01
-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false
[root@dev18 ~]#
使用jinfo -sysprops 12905打印Java系统属性信息
[root@dev18 ~]# jinfo -sysprops 12905
Attaching to process ID 12905, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.71-b01
java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 24.71-b01
sun.boot.library.path = /usr/java/jdk1.7.0_71/jre/lib/amd64
zookeeper.root.logger = INFO,CONSOLE
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
com.sun.management.jmxremote.local.only = false
path.separator = :
java.rmi.server.randomIDs = true
file.encoding.pkg = sun.io
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
sun.os.patch.level = unknown
sun.java.launcher = SUN_STANDARD
user.country = US
user.dir = /srv/zookeepers/zookeeper/bin
java.vm.specification.name = Java Virtual Machine Specification
java.runtime.version = 1.7.0_71-b14
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/java/jdk1.7.0_71/jre/lib/endorsed
zookeeper.log.dir = .
java.io.tmpdir = /tmp
line.separator =
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
java.specification.name = Java Platform API Specification
java.class.version = 51.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 2.6.32-573.el6.x86_64
user.home = /root
user.timezone = Asia/Chungking
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.7
user.name = root
java.class.path = /srv/zookeepers/zookeeper/bin/../build/classes:/srv/zookeepers/zookeeper/bin/../build/lib/*.jar:/srv/zookeepers/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/srv/zookeepers/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/srv/zookeepers/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/srv/zookeepers/zookeeper/bin/../lib/log4j-1.2.16.jar:/srv/zookeepers/zookeeper/bin/../lib/jline-0.9.94.jar:/srv/zookeepers/zookeeper/bin/../zookeeper-3.4.9.jar:/srv/zookeepers/zookeeper/bin/../src/java/lib/*.jar:/srv/zookeepers/zookeeper/bin/../conf:.:/usr/java/jdk1.7.0_71/jre/lib/rt.jar:/usr/java/jdk1.7.0_71/lib/dt.jar:/usr/java/jdk1.7.0_71/lib/tools.jar
com.sun.management.jmxremote =
java.vm.specification.version = 1.7
sun.arch.data.model = 64
sun.java.command = org.apache.zookeeper.server.quorum.QuorumPeerMain /srv/zookeepers/zookeeper/bin/../conf/zoo.cfg
java.home = /usr/java/jdk1.7.0_71/jre
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.7.0_71
java.ext.dirs = /usr/java/jdk1.7.0_71/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /usr/java/jdk1.7.0_71/jre/lib/resources.jar:/usr/java/jdk1.7.0_71/jre/lib/rt.jar:/usr/java/jdk1.7.0_71/jre/lib/sunrsasign.jar:/usr/java/jdk1.7.0_71/jre/lib/jsse.jar:/usr/java/jdk1.7.0_71/jre/lib/jce.jar:/usr/java/jdk1.7.0_71/jre/lib/charsets.jar:/usr/java/jdk1.7.0_71/jre/lib/jfr.jar:/usr/java/jdk1.7.0_71/jre/classes
java.vendor = Oracle Corporation
file.separator = /
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.cpu.isalist =
[root@dev18 ~]#
不指定option,同时打印JVM flags和Java系统属性~
如 jinfo 12905
[root@dev18 ~]# jinfo 12905
Attaching to process ID 12905, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.71-b01
Java System Properties:
java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 24.71-b01
sun.boot.library.path = /usr/java/jdk1.7.0_71/jre/lib/amd64
zookeeper.root.logger = INFO,CONSOLE
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
com.sun.management.jmxremote.local.only = false
path.separator = :
java.rmi.server.randomIDs = true
file.encoding.pkg = sun.io
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
sun.os.patch.level = unknown
sun.java.launcher = SUN_STANDARD
user.country = US
user.dir = /srv/zookeepers/zookeeper/bin
java.vm.specification.name = Java Virtual Machine Specification
java.runtime.version = 1.7.0_71-b14
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/java/jdk1.7.0_71/jre/lib/endorsed
zookeeper.log.dir = .
java.io.tmpdir = /tmp
line.separator =
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
java.specification.name = Java Platform API Specification
java.class.version = 51.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 2.6.32-573.el6.x86_64
user.home = /root
user.timezone = Asia/Chungking
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.7
user.name = root
java.class.path = /srv/zookeepers/zookeeper/bin/../build/classes:/srv/zookeepers/zookeeper/bin/../build/lib/*.jar:/srv/zookeepers/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/srv/zookeepers/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/srv/zookeepers/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/srv/zookeepers/zookeeper/bin/../lib/log4j-1.2.16.jar:/srv/zookeepers/zookeeper/bin/../lib/jline-0.9.94.jar:/srv/zookeepers/zookeeper/bin/../zookeeper-3.4.9.jar:/srv/zookeepers/zookeeper/bin/../src/java/lib/*.jar:/srv/zookeepers/zookeeper/bin/../conf:.:/usr/java/jdk1.7.0_71/jre/lib/rt.jar:/usr/java/jdk1.7.0_71/lib/dt.jar:/usr/java/jdk1.7.0_71/lib/tools.jar
com.sun.management.jmxremote =
java.vm.specification.version = 1.7
sun.arch.data.model = 64
sun.java.command = org.apache.zookeeper.server.quorum.QuorumPeerMain /srv/zookeepers/zookeeper/bin/../conf/zoo.cfg
java.home = /usr/java/jdk1.7.0_71/jre
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.7.0_71
java.ext.dirs = /usr/java/jdk1.7.0_71/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /usr/java/jdk1.7.0_71/jre/lib/resources.jar:/usr/java/jdk1.7.0_71/jre/lib/rt.jar:/usr/java/jdk1.7.0_71/jre/lib/sunrsasign.jar:/usr/java/jdk1.7.0_71/jre/lib/jsse.jar:/usr/java/jdk1.7.0_71/jre/lib/jce.jar:/usr/java/jdk1.7.0_71/jre/lib/charsets.jar:/usr/java/jdk1.7.0_71/jre/lib/jfr.jar:/usr/java/jdk1.7.0_71/jre/classes
java.vendor = Oracle Corporation
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:
-Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false
[root@dev18 ~]#
查看某个参数的值,jinfo -flag SurvivorRatio 12905
[root@dev18 ~]# jinfo -flag SurvivorRatio 12905
-XX:SurvivorRatio=8
[root@dev18 ~]#
我们通过-XX:+PrintFlagsFinal -version参数可以获取JVM的所有选项,因为这样的信息很多,我们只列出可管理 (manageable) 的参数信息,如:
[root@dev18 ~]# java -XX:+PrintFlagsFinal -version | grep manageable
intx CMSAbortablePrecleanWaitMillis = 100 {manageable}
intx CMSWaitDuration = 2000 {manageable}
bool HeapDumpAfterFullGC = false {manageable}
bool HeapDumpBeforeFullGC = false {manageable}
bool HeapDumpOnOutOfMemoryError = false {manageable}
ccstr HeapDumpPath = {manageable}
uintx MaxHeapFreeRatio = 100 {manageable}
uintx MinHeapFreeRatio = 0 {manageable}
bool PrintClassHistogram = false {manageable}
bool PrintClassHistogramAfterFullGC = false {manageable}
bool PrintClassHistogramBeforeFullGC = false {manageable}
bool PrintConcurrentLocks = false {manageable}
bool PrintGC = false {manageable}
bool PrintGCDateStamps = false {manageable}
bool PrintGCDetails = false {manageable}
bool PrintGCTimeStamps = false {manageable}
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
[root@dev18 ~]#
从上述信息中可以看到,MaxHeapFreeRatio的值为100,我们尝试去修改一下这个值,将其改成80~
修改MaxHeapFreeRatio的值,使用jinfo -flag MaxHeapFreeRatio=80 12905
然后查一下修改后MaxHeapFreeRatio的值已经为80~
[root@dev18 ~]# jinfo -flag MaxHeapFreeRatio 12905
-XX:MaxHeapFreeRatio=80
重新将MaxHeapFreeRatio设置为默认的100~
[root@dev18 ~]# jinfo -flag MaxHeapFreeRatio=100 12905
[root@dev18 ~]# jinfo -flag MaxHeapFreeRatio 12905
-XX:MaxHeapFreeRatio=100
[root@dev18 ~]#
本文使用jinfo命令工具给出相关的示例,有兴趣的读者可以动手操作一下~ :)