常用命令查找文档站点:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/index.html
-XX:+PrintFlagsInitial
输出所有参数的名称和默认值,默认不包括Diagnostic和Experimental的参数。可以配合
-XX:+UnlockDiagnosticVMOptions
和-XX:UnlockExperimentalVMOptions
使用
注意:这里列举出来的所有参数值,大体分为两类。一类是 “=” 的表示默认值;一类是 “ := ” 的表示是被用户或者JVM修改后的值。
JPS 命令
查看使用格式:
jps -help
usage: jps [-help]jps [-q] [-mlvV] [<hostid>]Definitions:<hostid>: <hostname>[:<port>]
1、查看java进程pid
$> jps -q
28680
23789
23651
2、查看 main 方法的参数
特别注意,在嵌入式jvm上可能是null
$> jps -m
28715 Jps -m
23789 BossMain
23651 Resin -socketwait 32768 -stdout /data/aoxj/resin/log/stdout.log -stderr /data/aoxj/resin/log/stderr.log
3、查看Java进程完整包名
# -l 输出应用程序main class的完整package名 或者 应用程序的jar文件完整路径名
$> jps -l
28729 sun.tools.jps.Jps
23789 com.asiainfo.aimc.bossbi.BossMain
23651 com.caucho.server.resin.Resin
4、查看传递给JVM的参数
$> jps -v
23789 BossMain
28802 Jps -Denv.class.path=/data/aoxj/bossbi/twsecurity/java/trustwork140.jar:/data/aoxj/bossbi/twsecurity/java/:/data/aoxj/bossbi/twsecurity/java/twcmcc.jar:/data/aoxj/jdk15/lib/rt.jar:/data/aoxj/jdk15/lib/tools.jar -Dapplication.home=/data/aoxj/jdk15 -Xms8m
23651 Resin -Xss1m -Dresin.home=/data/aoxj/resin -Dserver.root=/data/aoxj/resin -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl
JInfo 命令
jinfo
是 JDK
自带的命令,可以用来查看正在运行的 java
应用程序的扩展参数,包括Java System属性和JVM命令行参数;也可以动态的修改正在运行的 JVM
一些参数。当系统崩溃时,jinfo可以从core文件里面知道崩溃的Java应用程序的配置信息。基本语法是:jinfo [ option ] pid
Option:
选项 | 选项说明 |
---|---|
no option | 输出全部的参数和系统属性 |
-flag name | 输出对应名称的参数 |
-flag [+/-] name | 开启或者关闭对应名称的参数,只有被标记为 manageable 的参数才可以被动态修改 |
-flag name=value | 设定对应名称的参数 |
-flags | 输出全部的参数 |
-sysprops | 输出系统属性 |
1、jinfo -flags pid
#输入
jinfo -flags 7663#输出
Attaching to process ID 7663, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13
Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:MaxNewSize=1431306240 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=89128960 -XX:OldSize=179306496 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
Command line: -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49834:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8
2、jinfo -flag pid
其中,<name>
不包含上图的前缀 -XX:
等,不是 jinfo -flag -XX:NewSize 7663
,而是 jinfo -flag NewSize 7663
,不包含 -XX:
。
#输入
jinfo -flag NewSize 7663#输出
-XX:NewSize=89128960
3、jinfo -flag [+|-] pid
用于打开或关闭指定的参数
4、jinfo -sysprops pid
打印当前java进程中设定的系统环境参数
#输入
jinfo -sysprops 7663#输出
Attaching to process ID 7663, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13
java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.121-b13
sun.boot.library.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib
gopherProxySet = false
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
path.separator = :
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 = CN
user.dir = /Users/davis/workspace/intellij/cw-javaagent-demo
java.vm.specification.name = Java Virtual Machine Specification
java.runtime.version = 1.8.0_121-b13
java.awt.graphicsenv = sun.awt.CGraphicsEnvironment
os.arch = x86_64
java.endorsed.dirs = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/endorsed
line.separator =java.io.tmpdir = /var/folders/5n/6g464jd97r13x39t5w69ybfr0000gn/T/
java.vm.specification.vendor = Oracle Corporation
os.name = Mac OS X
sun.jnu.encoding = UTF-8
java.library.path = /Users/davis/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 10.15.5
http.nonProxyHosts = local|*.local|169.254/16|*.169.254/16
user.home = /Users/davis
user.timezone = Asia/Shanghai
java.awt.printerjob = sun.lwawt.macosx.CPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
user.name = davis
java.class.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/tools.jar:/Users/davis/workspace/intellij/cw-javaagent-demo/target/classes:/Users/davis/maven/net/sourceforge/jtds/jtds/1.2.6/jtds-1.2.6.jar:/Users/davis/maven/jstl/jstl/1.2/jstl-1.2.jar:/Users/davis/maven/taglibs/standard/1.1.2/standard-1.1.2.jar:/Users/davis/maven/org/mybatis/mybatis/3.2.8/mybatis-3.2.8.jar:/Users/davis/maven/org/mybatis/mybatis-spring/1.2.2/mybatis-spring-1.2.2.jar:/Users/davis/maven/redis/clients/jedis/2.5.2/jedis-2.5.2.jar:/Users/davis/maven/org/apache/commons/commons-pool2/2.0/commons-pool2-2.0.jar:/Users/davis/maven/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar:/Users/davis/maven/org/springframework/spring-aop/4.1.1.RELEASE/spring-aop-4.1.1.RELEASE.jar:/Users/davis/maven/aopalliance/aopalliance/1.0/aopalliance-1.0.jar:/Users/davis/maven/org/springframework/spring-aspects/4.1.1.RELEASE/spring-aspects-4.1.1.RELEASE.jar:/Users/davis/maven/org/aspectj/aspectjweaver/1.8.2/aspectjweaver-1.8.2.jar:/Users/davis/maven/org/springframework/spring-beans/4.1.1.RELEASE/spring-beans-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-context/4.1.1.RELEASE/spring-context-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-context-support/4.1.1.RELEASE/spring-context-support-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-core/4.1.1.RELEASE/spring-core-4.1.1.RELEASE.jar:/Users/davis/maven/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/Users/davis/maven/org/springframework/spring-expression/4.1.1.RELEASE/spring-expression-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-instrument/4.1.1.RELEASE/spring-instrument-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-instrument-tomcat/4.1.1.RELEASE/spring-instrument-tomcat-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-jdbc/4.1.1.RELEASE/spring-jdbc-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-jms/4.1.1.RELEASE/spring-jms-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-messaging/4.1.1.RELEASE/spring-messaging-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-orm/4.1.1.RELEASE/spring-orm-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-oxm/4.1.1.RELEASE/spring-oxm-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-test/4.1.1.RELEASE/spring-test-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-tx/4.1.1.RELEASE/spring-tx-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-web/4.1.1.RELEASE/spring-web-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-webmvc/4.1.1.RELEASE/spring-webmvc-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-webmvc-portlet/4.1.1.RELEASE/spring-webmvc-portlet-4.1.1.RELEASE.jar:/Users/davis/maven/org/springframework/spring-websocket/4.1.1.RELEASE/spring-websocket-4.1.1.RELEASE.jar:/Users/davis/maven/commons-fileupload/commons-fileupload/1.3.1/commons-fileupload-1.3.1.jar:/Users/davis/maven/commons-io/commons-io/2.2/commons-io-2.2.jar:/Users/davis/maven/commons-dbcp/commons-dbcp/1.2.2/commons-dbcp-1.2.2.jar:/Users/davis/maven/commons-pool/commons-pool/1.4/commons-pool-1.4.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = com.cw.tsb.jvm.JVMTest
java.home = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre
user.language = zh
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.lwawt.macosx.LWCToolkit
java.vm.info = mixed mode
java.version = 1.8.0_121
java.ext.dirs = /Users/davis/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
sun.boot.class.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/classes
java.vendor = Oracle Corporation
file.separator = /
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = UnicodeBig
sun.cpu.endian = little
socksNonProxyHosts = local|*.local|169.254/16|*.169.254/16
ftp.nonProxyHosts = local|*.local|169.254/16|*.169.254/16
sun.cpu.isalist =
Jstack命令
Usage:jstack [-l] <pid>(to connect to running process)jstack -F [-m] [-l] <pid>(to connect to a hung process)jstack [-m] [-l] <executable> <core>(to connect to a core file)
Options:-F 强制打印线程栈信息,在jstack <pid>命令没有响应的时候添加该选项可以实现强制打印-m (mix)混合模式,可以打印 Java 栈和本地方法栈-l (long listing)长列表模式. 额外打印关于锁的信息
线程状态转移图:
1、状态含义
Java语言定义了6种线程池状态:
- New:创建后尚未启动的线程处于这种状态,不会出现在Dump中。
- RUNNABLE:包括Running和Ready。线程开启start()方法,会进入该状态,在虚拟机内执行的。
- Waiting:无限的等待另一个线程的特定操作。
- Timed Waiting:有时限的等待另一个线程的特定操作。
- 阻塞(Blocked):在程序等待进入同步区域的时候,线程将进入这种状态,在等待监视器锁。
- 结束(Terminated):已终止线程的线程状态,线程已经结束执行。
Dump文件的线程状态一般其实就以下3种:
- RUNNABLE,线程处于执行中
- BLOCKED,线程被阻塞
- WAITING,线程正在等待
2、Monitor 监视锁
Monitor的工作原理图如下:
- 线程想要获取monitor,首先会进入Entry Set队列,它是Waiting Thread,线程状态是Waiting for monitor entry。
- 当某个线程成功获取对象的monitor后,进入Owner区域,它就是Active Thread。
- 如果线程调用了wait()方法,则会进入Wait Set队列,它会释放monitor锁,它也是Waiting Thread,线程状态in Object.wait()
- 如果其他线程调用 notify() / notifyAll() ,会唤醒Wait Set中的某个线程,该线程再次尝试获取monitor锁,成功即进入Owner区域。
3、Dump 文件分析关注重点
- runnable,线程处于执行中
- deadlock,死锁(重点关注)
- blocked,线程被阻塞 (重点关注)
- Parked,停止
- locked,对象加锁
- waiting,线程正在等待
- waiting to lock 等待上锁
- Object.wait(),对象等待中
- waiting for monitor entry 等待获取监视器(重点关注)
- Waiting on condition,等待资源(重点关注),最常见的情况是线程在等待网络的读写
Jmap 命令
jmap(Java Virtual Machine Memory Map)是JDK提供的一个可以生成Java虚拟机的堆转储快照dump文件的命令行工具。除此以外,jmap命令还可以查看finalize执行队列、Java堆和方法区的详细信息,比如空间使用率、当前使用的什么垃圾回收器、分代情况等等。
命令语法:
jmap [options] pid
命令参数说明:
option
:jmap命令的可选参数。如果没有指定这个参数,jinfo命令会显示Java虚拟机进程的内存映像信息,如下图:
pid
:要打印配置信息的Java虚拟机的进程ID。
想要要获取运行的Java虚拟机进程的列表,可以使用ps命令(Linux系统中)或tasklist命令(Windows系统中),如果Java虚拟机进程没有在单独的docker实例中运行,可以使用jps命令。
1、- help 查看堆信息
显示Java堆的如下信息:
- 被指定的垃圾回收算法的信息,包括垃圾回收算法的名称和垃圾回收算法的详细信息。
- 堆的配置信息,可能是由命令行选项指定,或者由Java虚拟机根据服务器配置选择的。
- 堆的内存空间使用信息,包括分代情况,每个代的总容量、已使用内存、可使用内存。如果某一代被继续细分(例如,年轻代),则包含细分的空间的内存使用信息。
2、- histo[:live] 查看堆对象统计信息
显示Java堆中对象的统计信息,包括:对象数量、占用内存大小(单位:字节)和类的完全限定名。比如:
要获得某个对象的大小,可以将其总大小除以该对象类型的数量。如果指定了live
参数,则只计算活动的对象。比如:
3、- clstats 查看类加载器统计信息
显示Java堆中元空间的类加载器的统计信息,包括:
- class_loader:当Java虚拟机运行时,类加载器对象的地址
- classes:已加载类的数量
- bytes:该类加载器加载的所有类的元数据所占的字节数
- parent_loader:父类加载器对象的地址,如果没有显示
null
。 - alive:是否存活的标识,表示类加载器对象是否将被垃圾回收。
- type:该类加载器的类名。
4、- finalizerinfo 查看等待线程
显示在F-Queue中等待Finalizer线程执行finalize方法的对象。比如:
5、- dump:[live,]format=b,file= 生成dump
生成Java虚拟机的堆转储快照dump文件。具体说明如下:
live
参数是可选的,如果指定,则只转储堆中的活动对象;如果没有指定,则转储堆中的所有对象。format=b
表示以hprof二进制格式转储Java堆的内存。file=<filename>
用于指定快照dump文件的文件名。
6、-F 强制模式
强制模式。如果指定的pid没有响应,可以配合-dump
或-histo
一起使用。此模式下,不支持live参数。比如:
Jstat 命令
命令用法:jstat [-命令选项] [vmid] [间隔时间/毫秒] [查询次数]
C:\Users\Administrator>jstat -help
Usage: jstat -help|-optionsjstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]Definitions:<option> An option reported by the -options option<vmid> Virtual Machine Identifier. A vmid takes the following form:<lvmid>[@<hostname>[:<port>]]Where <lvmid> is the local vm identifier for the targetJava virtual machine, typically a process id; <hostname> isthe name of the host running the target Java virtual machine;and <port> is the port number for the rmiregistry on thetarget host. See the jvmstat documentation for a more completedescription of the Virtual Machine Identifier.<lines> Number of samples between header lines.<interval> Sampling interval. The following forms are allowed:<n>["ms"|"s"]Where <n> is an integer and the suffix specifies the units asmilliseconds("ms") or seconds("s"). The default units are "ms".<count> Number of samples to take before terminating.-J<flag> Pass <flag> directly to the runtime system.
- option:参数选项
- -t:可以在打印的列加上Timestamp列,用于显示系统运行的时间
- -h:可以在周期性数据输出的时候,指定输出多少行以后输出一次表头
- vmid:Virtual Machine ID( 进程的 pid)
- interval:执行每次的间隔时间,单位为毫秒
- count:用于指定输出多少次记录,缺省则会一直打印
option 可以从下面参数中选择
jstat -options
- -class 用于查看类加载情况的统计
- -compiler 用于查看HotSpot中即时编译器编译情况的统计
- -gc 用于查看JVM中堆的垃圾收集情况的统计
- -gccapacity 用于查看新生代、老生代及持久代的存储容量情况
- -gcmetacapacity 显示metaspace的大小
- -gcnew 用于查看新生代垃圾收集的情况
- -gcnewcapacity 用于查看新生代存储容量的情况
- -gcold 用于查看老生代及持久代垃圾收集的情况
- -gcoldcapacity 用于查看老生代的容量
- -gcutil 显示垃圾收集信息
- -gccause 显示垃圾回收的相关信息(通-gcutil),同时显示最后一次仅当前正在发生的垃圾收集的原因
- -printcompilation 输出JIT编译的方法信息