IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> JDK内置命令行工具 -> 正文阅读

[大数据]JDK内置命令行工具

java命令行

序号接口
javaJava 应用的启动程序
javacJDK 内置的编译工具
javap反编译 class 文件的工具
javadoc根据 Java 代码和标准注释,自动生成相关的 API 说明文档
javahJNI 开发时, 根据 java 代码生成需要的 .h文件
extcheck检查某个 jar 文件和运行时扩展 jar 有没有版本冲突,很少使用
jdbJava Debugger ; 可以调试本地和远端程序,属于 JPDA 中的一个 demo 实现,供其他调试器参考。开发时很少使用
jdeps探测 class 或 jar 包需要的依赖
jar打包工具,可以将文件和目录打包成为 .jar 文件;.jar 文件本质上就是 zip 文件, 只是后缀不同。使用时按顺序对应好选项和参数即可。
keytool安全证书和密钥的管理工具; (支持生成、导入、导出等操作)
jarsignerJAR 文件签名和验证工具
policytool实际上这是一款图形界面工具, 管理本机的 Java 安全策略
jps/jinfo查看 java 进程
jstat查看 JVM 内部 gc 相关信息
jmap查看 heap 或类占用空间统计
jstack查看线程信息
jcmd执行 JVM 相关分析命令(整合命令)
jrunscript/jjs执行 js 命令

一. jps

1. jps:查看当前 java 进程

18724 NodeManager
18280 SecondaryNameNode
20651 HMaster
17357 QuorumPeerMain
20848 HRegionServer
17841 NameNode
400340 Kafka
18038 DataNode
18550 ResourceManager
702428 Jps
21466 Worker
21372 Master
22269 jar

2. jps -q: 只显示 java 进程号

18724
702447
18280
20651
17357
20848
17841
400340
18038
18550
21466
21372
22269

3. jps –l:输出主类或者 jar 的完全路径名

702410 sun.tools.jps.Jps
18724 org.apache.hadoop.yarn.server.nodemanager.NodeManager
18280 org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode
20651 org.apache.hadoop.hbase.master.HMaster
17357 org.apache.zookeeper.server.quorum.QuorumPeerMain
20848 org.apache.hadoop.hbase.regionserver.HRegionServer
17841 org.apache.hadoop.hdfs.server.namenode.NameNode
400340 kafka.Kafka
18038 org.apache.hadoop.hdfs.server.datanode.DataNode
18550 org.apache.hadoop.yarn.server.resourcemanager.ResourceManager
21466 org.apache.spark.deploy.worker.Worker
21372 org.apache.spark.deploy.master.Master
22269 api-test.jar

4. jps -m: 输出 main 方法的启动参数

18724 NodeManager
18280 SecondaryNameNode
20651 HMaster start
17357 QuorumPeerMain /opt/software/zookeeper-3.4.14/bin/../conf/zoo.cfg
20848 HRegionServer start
17841 NameNode
400340 Kafka ../config/server.properties
702489 Jps -m
18038 DataNode
18550 ResourceManager
21466 Worker --webui-port 8081 spark://172.29.38.64:7077
21372 Master --host 172.29.38.64 --port 7077 --webui-port 8081
22269 jar

5. jps -v: 输出jvm参数

18724 NodeManager -Dproc_nodemanager -Djava.net.preferIPv4Stack=true -Dyarn.log.dir=/opt/software/hadoop-3.2.2/logs -Dyarn.log.file=hadoop-root-nodemanager-ashen-friede.log -Dyarn.home.dir=/opt/software/hadoop-3.2.2 -Dyarn.root.logger=INFO,console -Djava.library.path=/opt/software/hadoop-3.2.2/lib/native -Dhadoop.log.dir=/opt/software/hadoop-3.2.2/logs -Dhadoop.log.file=hadoop-root-nodemanager-ashen-friede.log -Dhadoop.home.dir=/opt/software/hadoop-3.2.2 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy.file=hadoop-policy.xml -Dhadoop.security.logger=INFO,NullAppender
18280 SecondaryNameNode -Dproc_secondarynamenode -Djava.net.preferIPv4Stack=true -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS -Dyarn.log.dir=/opt/software/hadoop-3.2.2/logs -Dyarn.log.file=hadoop-root-secondarynamenode-ashen-friede.log -Dyarn.home.dir=/opt/software/hadoop-3.2.2 -Dyarn.root.logger=INFO,console -Djava.library.path=/opt/software/hadoop-3.2.2/lib/native -Dhadoop.log.dir=/opt/software/hadoop-3.2.2/logs -Dhadoop.log.file=hadoop-root-secondarynamenode-ashen-friede.log -Dhadoop.home.dir=/opt/software/hadoop-3.2.2 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy.file=hadoop-policy.xml
20651 HMaster -Dproc_master -XX:OnOutOfMemoryError=kill -9 %p -XX:+UseConcMarkSweepGC -XX:PermSize=128m -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=256m -Dhbase.log.dir=/opt/software/hbase-1.4.13/logs -Dhbase.log.file=hbase-root-master-ashen-friede.log -Dhbase.home.dir=/opt/software/hbase-1.4.13 -Dhbase.id.str=root -Dhbase.root.logger=INFO,RFA -Djava.library.path=/opt/software/hadoop-3.2.2/lib:/opt/software/hadoop-3.2.2/lib/native -Dhbase.security.logger=INFO,RFAS
17357 QuorumPeerMain -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false
20848 HRegionServer -Dproc_regionserver -XX:OnOutOfMemoryError=kill -9 %p -XX:+UseConcMarkSweepGC -XX:PermSize=128m -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=256m -Dhbase.log.dir=/opt/software/hbase-1.4.13/bin/../logs -Dhbase.log.file=hbase-root-regionserver-ashen-friede.log -Dhbase.home.dir=/opt/software/hbase-1.4.13/bin/.. -Dhbase.id.str=root -Dhbase.root.logger=INFO,RFA -Dhbase.security.logger=INFO,RFAS
17841 NameNode -Dproc_namenode -Djava.net.preferIPv4Stack=true -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS -Dyarn.log.dir=/opt/software/hadoop-3.2.2/logs -Dyarn.log.file=hadoop-root-namenode-ashen-friede.log -Dyarn.home.dir=/opt/software/hadoop-3.2.2 -Dyarn.root.logger=INFO,console -Djava.library.path=/opt/software/hadoop-3.2.2/lib/native -Dhadoop.log.dir=/opt/software/hadoop-3.2.2/logs -Dhadoop.log.file=hadoop-root-namenode-ashen-friede.log -Dhadoop.home.dir=/opt/software/hadoop-3.2.2 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy.file=hadoop-policy.xml
400340 Kafka -Xmx1G -Xms1G -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:MaxInlineLevel=15 -Djava.awt.headless=true -Xloggc:/opt/software/kafka_2.12-3.0.0/bin/../logs/kafkaServer-gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dkafka.logs.dir=/opt/software/kafka_2.12-3.0.0/bin/../logs -Dlog4j.configuration=file:./../config/log4j.properties
702527 Jps -Denv.class.path=.:/usr/java/jdk1.8.0_151/lib/dt.jar:/usr/java/jdk1.8.0_151/lib/tools.jar:/opt/software/hadoop-3.2.2/lib/*.jar:/opt/software/hadoop-3.2.2/share/hadoop/client/*.jar:/opt/software/hadoop-3.2.2/share/hadoop/common/*.jar:/opt/software/hadoop-3.2.2/share/hadoop/hdfs/*.jar:/opt/software/hadoop-3.2.2/share/hadoop/mapreduce/*.jar:/opt/software/hadoop-3.2.2/share/hadoop/yarn/*.jar:/opt/software/hbase-1.4.13/lib/ -Dapplication.home=/usr/java/jdk1.8.0_151 -Xms8m
18038 DataNode -Dproc_datanode -Djava.net.preferIPv4Stack=true -Dhadoop.security.logger=ERROR,RFAS -Dyarn.log.dir=/opt/software/hadoop-3.2.2/logs -Dyarn.log.file=hadoop-root-datanode-ashen-friede.log -Dyarn.home.dir=/opt/software/hadoop-3.2.2 -Dyarn.root.logger=INFO,console -Djava.library.path=/opt/software/hadoop-3.2.2/lib/native -Dhadoop.log.dir=/opt/software/hadoop-3.2.2/logs -Dhadoop.log.file=hadoop-root-datanode-ashen-friede.log -Dhadoop.home.dir=/opt/software/hadoop-3.2.2 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy.file=hadoop-policy.xml
18550 ResourceManager -Dproc_resourcemanager -Djava.library.path=/opt/software/hadoop-3.2.2/lib:/opt/software/hadoop-3.2.2/lib/native -Dservice.libdir=/opt/software/hadoop-3.2.2/share/hadoop/yarn,/opt/software/hadoop-3.2.2/share/hadoop/yarn/lib,/opt/software/hadoop-3.2.2/share/hadoop/hdfs,/opt/software/hadoop-3.2.2/share/hadoop/hdfs/lib,/opt/software/hadoop-3.2.2/share/hadoop/common,/opt/software/hadoop-3.2.2/share/hadoop/common/lib -Dyarn.log.dir=/opt/software/hadoop-3.2.2/logs -Dyarn.log.file=hadoop-root-resourcemanager-ashen-friede.log -Dyarn.home.dir=/opt/software/hadoop-3.2.2 -Dyarn.root.logger=INFO,console -Dhadoop.log.dir=/opt/software/hadoop-3.2.2/logs -Dhadoop.log.file=hadoop-root-resourcemanager-ashen-friede.log -Dhadoop.home.dir=/opt/software/hadoop-3.2.2 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy.file=hadoop-policy.xml -Dhadoop.security.logger=INFO,NullAppender
21466 Worker -Xmx1g
21372 Master -Xmx1g
22269 jar

二.jinfo

1. jinfo -[pid]

输出全部的参数和系统属性

Attaching to process ID 22269, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Java System Properties:

java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.151-b12
sun.boot.library.path = /usr/java/jdk1.8.0_151/jre/lib/amd64
java.protocol.handler.pkgs = org.springframework.boot.loader
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 = US
user.dir = /server/api-test
java.vm.specification.name = Java Virtual Machine Specification
PID = 22269
java.runtime.version = 1.8.0_151-b12
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/java/jdk1.8.0_151/jre/lib/endorsed
line.separator = 

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 = 4.18.0-348.2.1.el8_5.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 = /tmp/tomcat.7513305814779239686.8086
user.name = root
java.class.path = api-test.jar
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = api-test.jar
java.home = /usr/java/jdk1.8.0_151/jre
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.8.0_151
java.ext.dirs = /usr/java/jdk1.8.0_151/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /usr/java/jdk1.8.0_151/jre/lib/resources.jar:/usr/java/jdk1.8.0_151/jre/lib/rt.jar:/usr/java/jdk1.8.0_151/jre/lib/sunrsasign.jar:/usr/java/jdk1.8.0_151/jre/lib/jsse.jar:/usr/java/jdk1.8.0_151/jre/lib/jce.jar:/usr/java/jdk1.8.0_151/jre/lib/charsets.jar:/usr/java/jdk1.8.0_151/jre/lib/jfr.jar:/usr/java/jdk1.8.0_151/jre/classes
java.awt.headless = true
java.vendor = Oracle Corporation
catalina.base = /tmp/tomcat.7513305814779239686.8086
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=3 -XX:InitialHeapSize=127926272 -XX:MaxHeapSize=2038431744 -XX:MaxNewSize=679477248 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=42467328 -XX:OldSize=85458944 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC 
Command line:  

2. jinfo -flag [name] pid

输出对应名称的参数:

jinfo -flag MaxHeapSize 22269

输出:

[root@ashen-friede ~]# jinfo -flag MaxHeapSize 22269
-XX:MaxHeapSize=2038431744

3. jinfo -flag [+|-][name] pid

在不重启虚拟机的情况下,可以动态的修改 jvm 的参数。尤其在线上的环境特别有用。

减少配置

jinfo -flag -PrintGC 22269
[root@ashen-friede ~]# jinfo -flag -PrintGC 22269
[root@ashen-friede ~]# jinfo -flag PrintGC 22269
-XX:-PrintGC

增加配置

jinfo -flag +PrintGC 22269
[root@ashen-friede ~]# jinfo -flag +PrintGC 22269
[root@ashen-friede ~]# jinfo -flag PrintGC 22269
-XX:+PrintGC

4. jinfo -flag [name]=[value] pid

在不重启虚拟机的情况下,可以动态的修改 jvm 的参数值。尤其在线上的环境特别有用。

jinfo -flag HeapDumpPath=/mnt/test.hprof 22269

输出:

[root@ashen-friede ~]# jinfo -flag HeapDumpPath 22269
-XX:HeapDumpPath=
[root@ashen-friede ~]# jinfo -flag HeapDumpPath=/mnt/test.hprof 22269
[root@ashen-friede ~]# jinfo -flag HeapDumpPath 22269
-XX:HeapDumpPath=/mnt/test.hprof

并不是所有的参数都支持动态修改

5. jinfo -flags pid

输出全部的参数

jinfo -flags 22269

输出:

[root@ashen-friede ~]# jinfo -flags 22269
Attaching to process ID 22269, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Non-default VM flags: -XX:CICompilerCount=3 -XX:HeapDumpPath=null -XX:InitialHeapSize=127926272 -XX:MaxHeapSize=2038431744 -XX:MaxNewSize=679477248 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=42467328 -XX:OldSize=85458944 -XX:-PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC 
Command line:  

6. jinfo -sysprops pid

jinfo -sysprops 22269

输出:

[root@ashen-friede ~]# jinfo -sysprops 22269
Attaching to process ID 22269, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.151-b12
sun.boot.library.path = /usr/java/jdk1.8.0_151/jre/lib/amd64
java.protocol.handler.pkgs = org.springframework.boot.loader
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 = US
user.dir = /server/api-test
java.vm.specification.name = Java Virtual Machine Specification
PID = 22269
java.runtime.version = 1.8.0_151-b12
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/java/jdk1.8.0_151/jre/lib/endorsed
line.separator = 

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 = 4.18.0-348.2.1.el8_5.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 = /tmp/tomcat.7513305814779239686.8086
user.name = root
java.class.path = api-test.jar
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = api-test.jar
java.home = /usr/java/jdk1.8.0_151/jre
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.8.0_151
java.ext.dirs = /usr/java/jdk1.8.0_151/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /usr/java/jdk1.8.0_151/jre/lib/resources.jar:/usr/java/jdk1.8.0_151/jre/lib/rt.jar:/usr/java/jdk1.8.0_151/jre/lib/sunrsasign.jar:/usr/java/jdk1.8.0_151/jre/lib/jsse.jar:/usr/java/jdk1.8.0_151/jre/lib/jce.jar:/usr/java/jdk1.8.0_151/jre/lib/charsets.jar:/usr/java/jdk1.8.0_151/jre/lib/jfr.jar:/usr/java/jdk1.8.0_151/jre/classes
java.awt.headless = true
java.vendor = Oracle Corporation
catalina.base = /tmp/tomcat.7513305814779239686.8086
file.separator = /
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.cpu.isalist = 

三. jstat

对Java应用程序的资源和性能进行实时的命令行的监控,包括了对Heap size和垃圾回收状况的监控.

1.jstat -class [pid]

显示加载class的数量,及所占空间等信息。

jstat -class 22269
[root@ashen-friede ~]# jstat -class 22269
Loaded  Bytes  Unloaded  Bytes     Time   
  7604 13807.4        1     0.9       4.30
说明
Loaded装载的类的数量
Bytes装载类所占用的字节数
Unloaded卸载类的数量
Bytes卸载类的字节数
Time装载和卸载类所花费的时间

2.jstat -compiler [pid]

JIT 即时编译器相关的统计信息

jstat -compiler 22269

输出:

[root@ashen-friede ~]# jstat -compiler 22269
Compiled Failed Invalid   Time   FailedType FailedMethod
    6540      0       0    21.65          0         
说明
Compiled编译任务执行数量
Failed编译任务执行失败数量
Invalid编译任务执行失效数量
Time编译任务消耗时间
FailedType最后一个编译失败任务的类型
FailedMethod最后一个编译失败任务所在的类及方法

3.jstat -gc [pid] [time默认毫秒]

显示gc的信息,查看gc的次数,及时间

3.1 打印 gc 信息
jstat -gc 22269

输出:

[root@ashen-friede ~]# jstat -gc 22269
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
说明
S0C年轻代中第一个survivor(幸存区)的容量(字节)
S1C年轻代中第二个survivor(幸存区)的容量(字节)
S0U年轻代中第一个survivor(幸存区)目前已使用空间(字节)
S1U年轻代中第二个survivor(幸存区)目前已使用空间(字节)
EC年轻代中Eden(伊甸园)的容量(字节)
EU年轻代中Eden(伊甸园)目前已使用空间(字节)
OCOld代的容量(字节)
OUOld代目前已使用空间(字节)
PCPerm(持久代)的容量(字节)
PUPerm(持久代)目前已使用空间(字节)
YGC从应用程序启动到采样时年轻代中gc次数
YGCT从应用程序启动到采样时年轻代中gc所用时间(s)
FGC从应用程序启动到采样时old代(全gc)gc次数
FGCT从应用程序启动到采样时old代(全gc)gc所用时间(s)
GCT从应用程序启动到采样时gc用的总时间(s)
3.2 每隔 5s 打印 gc 信息
jstat -gc 22269 5000

输出:

[root@ashen-friede ~]# jstat -gc 22269 5000
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
5120.0 8192.0 5008.2  0.0   256000.0 145335.5  97280.0    15881.1   43056.0 41137.6 5424.0 5067.7     12    0.129   2      0.180    0.309
3.3 每隔 10 行打印表头, 每隔 1s 打印一次gc信息 并打印 jar 包启动时间
 jstat -gc -h 10 -t 703842 1s

输出:

[root@ashen-friede mnt]# jstat -gc -h 10 -t 703842 1s
Timestamp        S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
         8089.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8090.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8091.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8092.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8093.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8094.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8095.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8096.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8097.5 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8098.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
Timestamp        S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
         8099.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8100.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8101.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8102.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8103.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420
         8104.6 9216.0 9728.0  0.0    0.0   188928.0 27366.1   107520.0   15647.1   38912.0 36872.3 5120.0 4726.8      9    0.115   3      0.304    0.420

参数接口
-h [count]每隔十行打印一次表头
-t打印时间, 该时间为系统启动的秒数

4.jstat -gccapacity [pid]

打印各个内存池分代空间的容量

jstat -gccapacity 703842
[root@ashen-friede api-test]# jstat -gccapacity 703842
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC 
 41472.0 663552.0 208384.0 6144.0 9728.0 188928.0    83456.0  1327104.0    87040.0    87040.0      0.0 1079296.0  35456.0      0.0 1048576.0   4736.0      8     2
说明
NGCMN年轻代(young)中初始化(最小)的大小(字节)
NGCMX年轻代(young)的最大容量(字节)
NGC年轻代(young)中当前的容量(字节)
S0C年轻代中第一个survivor(幸存区)的容量(字节)
S1C年轻代中第二个survivor(幸存区)的容量(字节)
EC年轻代中Eden(伊甸园)的容量(字节)
OGCMNold代中初始化(最小)的大小(字节)
OGCMXold代的最大容量(字节)
OGCold代当前新生成的容量(字节)
OCOld代的容量(字节)
PGCMNperm代中初始化(最小)的大小(字节)
PGCMXperm代的最大容量(字节)
PGCperm代当前新生成的容量(字节)
PCPerm(持久代)的容量(字节)
YGC从应用程序启动到采样时年轻代中gc次数
FGC从应用程序启动到采样时old代(全gc)gc次数

其他用法与 -gc 类似

5.jstat -gcutil [pid]

GC 相关区域的使用率(utilization)统计

jstat -gcutil 703842

输出:

[root@ashen-friede api-test]# jstat -gcutil 703842
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  65.87  18.50  95.85  91.98      8    0.101     2    0.194    0.294
说明
S0年轻代中第一个survivor(幸存区)已使用的占当前容量百分比
S1年轻代中第二个survivor(幸存区)已使用的占当前容量百分比
E年轻代中Eden(伊甸园)已使用的占当前容量百分比
Oold代已使用的占当前容量百分比
Pperm代已使用的占当前容量百分比
YGC从应用程序启动到采样时年轻代中gc次数
YGCT从应用程序启动到采样时年轻代中gc所用时间(s)
FGC从应用程序启动到采样时old代(全gc)gc次数
FGCT从应用程序启动到采样时old代(全gc)gc所用时间(s)
GCT从应用程序启动到采样时gc用的总时间(s)

其他用法与 -gc 类似

6.jstat -gccause [pid]

看上次 GC,本次 GC(如果正在 GC 中)的原因, 其他输出和 -gcutil 选项一致

jstat -gccause 703842

输出:

[root@ashen-friede api-test]# jstat -gccause 703842
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                 
  0.00   0.00  65.87  18.50  95.85  91.98      8    0.101     2    0.194    0.294 Metadata GC Threshold No GC 
说明
LGCC上次垃圾回收的原因
GCC当前垃圾回收的原因

其他参数与 -gcutil 一致

7.jstat -gcnew [pid]

年轻代的统计信息。(New = Young = Eden + S0 + S1) -gcnewcapacity 年轻代空间大小统计

jstat -gcnew 703842

输出:

[root@ashen-friede api-test]# jstat -gcnew 703842
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
6144.0 9728.0    0.0    0.0  3  15 9728.0 188928.0 124448.4      8    0.101
说明
S0C年轻代中第一个survivor(幸存区)的容量(字节)
S1C年轻代中第二个survivor(幸存区)的容量(字节)
S0U年轻代中第一个survivor(幸存区)目前已使用空间(字节)
S1U年轻代中第二个survivor(幸存区)目前已使用空间(字节)
TT持有次数限制
MTT最大持有次数限制
EC年轻代中Eden(伊甸园)的容量(字节)
EU年轻代中Eden(伊甸园)目前已使用空间(字节)
YGC从应用程序启动到采样时年轻代中gc次数
YGCT从应用程序启动到采样时年轻代中gc所用时间(s)

8.jstat -gcold [pid]

老年代和元数据区的行为统计

jstat -gcold 703842

输出:

[root@ashen-friede api-test]# jstat -gcold 703842
   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   
 35456.0  33983.7   4736.0   4356.0     87040.0     16105.1      8     2    0.194    0.294
说明
MC方法区大小
MU方法区使用大小
CCSC压缩类空间大小
CCSU压缩类空间使用大小
OCOld代的容量(字节)
OUOld代目前已使用空间(字节)
YGC从应用程序启动到采样时年轻代中gc次数
FGC从应用程序启动到采样时old代(全gc)gc次数
FGCT从应用程序启动到采样时old代(全gc)gc所用时间(s)
GCT从应用程序启动到采样时gc用的总时间(s)

9.jstat -gcoldcapacity [pid]

old代空间大小统计

jstat -gcoldcapacity 703842

输出:

[root@ashen-friede api-test]# jstat -gcoldcapacity 703842
   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT   
    83456.0   1327104.0     87040.0     87040.0     8     2    0.194    0.294
说明
OGCMN老年代最小容量
OGCMX老年代最大容量
OGC当前老年代大小
OC老年代大小
YGC年轻代垃圾回收次数
FGC老年代垃圾回收次数
FGCT老年代垃圾回收消耗时间
GCT垃圾回收消耗总时间

10.jstat -gcmetacapacity [pid]

meta 区大小统计

jstat -gcmetacapacity 703842

输出:

[root@ashen-friede api-test]# jstat -gcmetacapacity 703842
   MCMN       MCMX        MC       CCSMN      CCSMX       CCSC     YGC   FGC    FGCT     GCT   
       0.0  1079296.0    35456.0        0.0  1048576.0     4736.0     8     2    0.194    0.294

说明
MCMN最小元数据容量
MCMX最大元数据容量
MC当前元数据空间大小
CCSMN最小压缩类空间大小
CCSMX最大压缩类空间大小
CCSC当前压缩类空间大小
YGC年轻代垃圾回收次数
FGC老年代垃圾回收次数
FGCT老年代垃圾回收消耗时间
GCT垃圾回收消耗总时间

11.jstat -printcompilation [pid]

打印 JVM 编译统计信息

jstat -printcompilation 703842

输出:

[root@ashen-friede api-test]# jstat -printcompilation 703842
Compiled  Size  Type Method
    3650   1020    1 sun/nio/ch/SelectorImpl lockAndDoSelect
说明
Compiled最近编译方法的数量
Size最近编译方法的字节码数量
Type最近编译方法的编译类型。
Method方法名标识。

四. jmap

jmap命令是一个可以输出所有内存中对象的工具,甚至可以将 JVM 中的heap,以二进制输出成文本。打印出某个java进程(使用pid)内存内的,所有对象的情况(产生那些对象,及其数量)。

1.jmap -heap [pid]

打印堆内存(/内存池)的配置和使用信息。

jmap -heap 703842

输出:

[root@ashen-friede api-test]# jmap -heap 703842
Attaching to process ID 703842, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 2038431744 (1944.0MB)
   NewSize                  = 42467328 (40.5MB)
   MaxNewSize               = 679477248 (648.0MB)
   OldSize                  = 85458944 (81.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 193462272 (184.5MB)
   used     = 127435120 (121.53160095214844MB)
   free     = 66027152 (62.96839904785156MB)
   65.87078642392869% used
From Space:
   capacity = 6291456 (6.0MB)
   used     = 0 (0.0MB)
   free     = 6291456 (6.0MB)
   0.0% used
To Space:
   capacity = 9961472 (9.5MB)
   used     = 0 (0.0MB)
   free     = 9961472 (9.5MB)
   0.0% used
PS Old Generation
   capacity = 89128960 (85.0MB)
   used     = 16491624 (15.727638244628906MB)
   free     = 72637336 (69.2723617553711MB)
   18.503103817210476% used

16170 interned Strings occupying 2168672 bytes.

2.jmap -histo [pid]

查看堆内存(histogram)中的对象数量,大小

jmap -histo 703842
说明
num序号
instances实例个数
bytes字节数
class name类名

3.jmap -dump:format=b,file=[filename].hprof [pid]

Dump 堆内存到 hprof 文件

jmap -dump:format=b,file=/mnt/test.hprof 703842

然后可以使用 jvisualvm 类似的工具查看

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xkqpSEHU-1647412936654)(3.png)]

五. jstack

jstack是java虚拟机自带的一种堆栈跟踪工具。jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息。

1.jstack -l [pid]

长列表模式,将线程相关的 locks 信息一起输出,比如持有的锁,等待的锁,死锁等信息

jstack -l 703842
[root@ashen-friede mnt]# jstack -l 703842
2022-03-11 13:22:31
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.151-b12 mixed mode):

"Attach Listener" #31 daemon prio=9 os_prio=0 tid=0x00007ff74c001000 nid=0xac04b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"DestroyJavaVM" #30 prio=5 os_prio=0 tid=0x00007ff784009800 nid=0xabd63 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"http-nio-8086-AsyncTimeout" #28 daemon prio=5 os_prio=0 tid=0x00007ff784dc3800 nid=0xabd84 waiting on condition [0x00007ff75028d000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1113)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-Acceptor-0" #27 daemon prio=5 os_prio=0 tid=0x00007ff784b29000 nid=0xabd83 runnable [0x00007ff75038e000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
	- locked <0x00000000de7a5c18> (a java.lang.Object)
	at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:446)
	at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:70)
	at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:95)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-ClientPoller-1" #26 daemon prio=5 os_prio=0 tid=0x00007ff784c3b000 nid=0xabd82 runnable [0x00007ff75048f000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
	at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
	at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000de905e98> (a sun.nio.ch.Util$3)
	- locked <0x00000000de905e88> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000de905d70> (a sun.nio.ch.EPollSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:742)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-ClientPoller-0" #25 daemon prio=5 os_prio=0 tid=0x00007ff784c39000 nid=0xabd81 runnable [0x00007ff750590000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
	at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
	at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000de8f5570> (a sun.nio.ch.Util$3)
	- locked <0x00000000de8f5560> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000de8f5448> (a sun.nio.ch.EPollSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:742)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-10" #24 daemon prio=5 os_prio=0 tid=0x00007ff784c37000 nid=0xabd80 waiting on condition [0x00007ff750691000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-9" #23 daemon prio=5 os_prio=0 tid=0x00007ff784c31000 nid=0xabd7f waiting on condition [0x00007ff750792000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-8" #22 daemon prio=5 os_prio=0 tid=0x00007ff784c2f000 nid=0xabd7e waiting on condition [0x00007ff750893000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-7" #21 daemon prio=5 os_prio=0 tid=0x00007ff784c2d000 nid=0xabd7d waiting on condition [0x00007ff750994000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-6" #20 daemon prio=5 os_prio=0 tid=0x00007ff784db9800 nid=0xabd7c waiting on condition [0x00007ff750a95000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-5" #19 daemon prio=5 os_prio=0 tid=0x00007ff784db7800 nid=0xabd7b waiting on condition [0x00007ff750b96000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-4" #18 daemon prio=5 os_prio=0 tid=0x00007ff784db6000 nid=0xabd7a waiting on condition [0x00007ff750c97000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-3" #17 daemon prio=5 os_prio=0 tid=0x00007ff784b34000 nid=0xabd79 waiting on condition [0x00007ff750d98000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-2" #16 daemon prio=5 os_prio=0 tid=0x00007ff784b33000 nid=0xabd78 waiting on condition [0x00007ff750e99000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"http-nio-8086-exec-1" #15 daemon prio=5 os_prio=0 tid=0x00007ff78481e000 nid=0xabd77 waiting on condition [0x00007ff750f9a000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000de7fa610> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"NioBlockingSelector.BlockPoller-1" #14 daemon prio=5 os_prio=0 tid=0x00007ff784c28800 nid=0xabd76 runnable [0x00007ff752476000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
	at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
	at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000de7a8860> (a sun.nio.ch.Util$3)
	- locked <0x00000000de7a87d8> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000de7a8410> (a sun.nio.ch.EPollSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:298)

   Locked ownable synchronizers:
	- None

"container-0" #13 prio=5 os_prio=0 tid=0x00007ff784f35000 nid=0xabd75 waiting on condition [0x00007ff7520ac000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.catalina.core.StandardServer.await(StandardServer.java:408)
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer$1.run(TomcatWebServer.java:181)

   Locked ownable synchronizers:
	- None

"ContainerBackgroundProcessor[StandardEngine[Tomcat]]" #12 daemon prio=5 os_prio=0 tid=0x00007ff784f2a000 nid=0xabd74 waiting on condition [0x00007ff75372d000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1365)
	at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
	- None

"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007ff7841b5000 nid=0xabd6f runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007ff784187800 nid=0xabd6e waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007ff784186000 nid=0xabd6d waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007ff784183000 nid=0xabd6c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007ff784181800 nid=0xabd6b runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007ff78414a800 nid=0xabd6a in Object.wait() [0x00007ff76c13c000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000000086932c90> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
	- locked <0x0000000086932c90> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

   Locked ownable synchronizers:
	- None

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007ff784146000 nid=0xabd69 in Object.wait() [0x00007ff76c23d000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000000086932c60> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:502)
	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
	- locked <0x0000000086932c60> (a java.lang.ref.Reference$Lock)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

   Locked ownable synchronizers:
	- None

"VM Thread" os_prio=0 tid=0x00007ff78413e800 nid=0xabd68 runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007ff78401f000 nid=0xabd64 runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007ff784021000 nid=0xabd65 runnable 

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007ff784022800 nid=0xabd66 runnable 

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007ff784024800 nid=0xabd67 runnable 

"VM Periodic Task Thread" os_prio=0 tid=0x00007ff7841b7800 nid=0xabd70 waiting on condition 

JNI global references: 1085

2.jstack -F [pid]

强制执行 thread dump,可在 Java 进程卡死(hung 住)时使用,此选项可能需要系统权限。

jstack -F 703842

输出:

Attaching to process ID 703842, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Deadlock Detection:

No deadlocks found.

Thread 704587: (state = BLOCKED)


Thread 703843: (state = BLOCKED)


Thread 703876: (state = BLOCKED)
 - java.lang.Thread.sleep(long) @bci=0 (Compiled frame; information may be imprecise)
 - org.apache.coyote.AbstractProtocol$AsyncTimeout.run() @bci=10, line=1113 (Interpreted frame)
 - java.lang.Thread.run() @bci=11, line=748 (Interpreted frame)


Thread 703875: (state = IN_NATIVE)
 - sun.nio.ch.ServerSocketChannelImpl.accept0(java.io.FileDescriptor, java.io.FileDescriptor, java.net.InetSocketAddress[]) @bci=0 (Interpreted frame)
 - sun.nio.ch.ServerSocketChannelImpl.accept(java.io.FileDescriptor, java.io.FileDescriptor, java.net.InetSocketAddress[]) @bci=4, line=422 (Interpreted frame)
 - sun.nio.ch.ServerSocketChannelImpl.accept() @bci=130, line=250 (Interpreted frame)
 - org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept() @bci=4, line=446 (Interpreted frame)
 - org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept() @bci=1, line=70 (Interpreted frame)
 - org.apache.tomcat.util.net.Acceptor.run() @bci=98, line=95 (Interpreted frame)
 - java.lang.Thread.run() @bci=11, line=748 (Interpreted frame)

3.jstack -m [pid]

混合模式(mixed mode),将 Java 帧和native 帧一起输出,此选项可能需要系统权限。

jstack -m 703842

输出:

Attaching to process ID 703842, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.151-b12
Deadlock Detection:

No deadlocks found.

----------------- 703843 -----------------
----------------- 703844 -----------------
----------------- 703845 -----------------
----------------- 703846 -----------------
----------------- 703847 -----------------
----------------- 703848 -----------------
----------------- 703849 -----------------
----------------- 703850 -----------------
----------------- 703851 -----------------
0x00007ff78cc4ecd6	do_futex_wait.constprop.1 + 0x36
----------------- 703852 -----------------
----------------- 703853 -----------------
----------------- 703854 -----------------
----------------- 703855 -----------------
----------------- 703856 -----------------
----------------- 703860 -----------------
----------------- 703861 -----------------
----------------- 703862 -----------------
0x00007ff78c55c0f7	epoll_wait + 0x57
----------------- 703863 -----------------
----------------- 703864 -----------------
----------------- 703865 -----------------
----------------- 703866 -----------------
----------------- 703867 -----------------
----------------- 703868 -----------------
----------------- 703869 -----------------
----------------- 703870 -----------------
----------------- 703871 -----------------
----------------- 703872 -----------------
----------------- 703873 -----------------
0x00007ff78c55c0f7	epoll_wait + 0x57
0x5a5f00657275736f		????????
----------------- 703874 -----------------
0x00007ff78c55c0f7	epoll_wait + 0x57
0x7645636f6c657235		????????
----------------- 703875 -----------------
0x00007ff78cc4fb07	__libc_accept + 0x47
----------------- 703876 -----------------
----------------- 704587 -----------------
0x00007ff78cc4fb07	__libc_accept + 0x47
----------------- 703842 -----------------
0x00007ff78cc4766d	__GI___pthread_timedjoin_ex + 0x18d

4.jstack -l [pid] | grep ‘java.lang.Thread.State’ | wc -l

统计现场个数

jstack -l 703842 | grep 'java.lang.Thread.State' | wc -l

输出:

[root@ashen-friede mnt]# jstack -l 703842 | grep 'java.lang.Thread.State' | wc -l
26

5.查看 linux 系统占用 cpu 较高的线程信息

5.1 top 查看cpu占用高进程

[root@ashen-friede mnt]# top
top - 13:40:26 up 50 days,  2:36,  2 users,  load average: 1.13, 1.07, 1.05
Tasks: 152 total,   1 running, 144 sleeping,   7 stopped,   0 zombie
%Cpu(s): 16.5 us,  9.7 sy,  0.0 ni, 73.4 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
MiB Mem :   7768.1 total,    637.5 free,   6313.4 used,    817.3 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   1186.9 avail Mem 
 Unknown command - try 'h' for help 
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                  
 226357 root      20   0  985480   6032   1376 S  99.7   0.1   4380:57 python3.7                                                                                                                                
 500552 root      20   0  989540  34172   3048 S   1.0   0.4 111:25.19 python3.7                                                                                                                                
  23077 root       1 -19 1011436  65836      4 S   0.7   0.8 449:53.73 jfsmount                                                                                                                                 
 495454 root      10 -10  184880  31432   8340 S   0.7   0.4 203:02.64 AliYunDun                                                                                                                                
 500551 root      20   0  807252  32568   4012 S   0.7   0.4 111:04.12 python3.7                                                                                                                                
  17841 root      20   0 3814460 291508  10268 S   0.3   3.7  48:38.10 java                                                                                                                                     
  18724 root      20   0 3816268 337620   9388 S   0.3   4.2  96:47.51 java                                                                                                                                     
  20848 root      20   0 3887612 292104   9724 S   0.3   3.7 129:23.67 java                                                                                                                                     
  21963 root      20   0 1322024  26828   6688 S   0.3   0.3 161:40.04 exe                                                                                                                                      
 400340 root      20   0 5937232   1.5g   9608 S   0.3  19.2 103:00.82 java                                                                                                                                     
 704190 root      20   0       0      0      0 I   0.3   0.0   0:03.53 kworker/3:3-events                                                                                                                       
      1 root      20   0  246524   6668   3732 S   0.0   0.1   0:55.32 systemd                                                                                                                                  
      2 root      20   0       0      0      0 S   0.0   0.0   0:01.20 kthreadd                                                                                                                                 
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                                                                                                                   
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                                                                                                                               
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri                                                                                                              
      9 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq                                                                                                                             
     10 root      20   0       0      0      0 S   0.0   0.0   0:04.15 ksoftirqd/0                                                                                                                              
     11 root      20   0       0      0      0 I   0.0   0.0  23:15.05 rcu_sched                                                                                                                                
     12 root      rt   0       0      0      0 S   0.0   0.0   0:00.05 migration/0                                                                                                                              
     13 root      rt   0       0      0      0 S   0.0   0.0   0:00.03 watchdog/0                                                                                                                               
     14 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/0                                                                                                                                  
     15 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/1                                                                                                                                  
     16 root      rt   0       0      0      0 S   0.0   0.0   0:02.14 watchdog/1                                                                                                                               
     17 root      rt   0       0      0      0 S   0.0   0.0   0:00.04 migration/1                                                                                                                              
     18 root      20   0       0      0      0 S   0.0   0.0   0:02.63 ksoftirqd/1                                                                                                                              
     20 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/1:0H-events_highpri                                                                                                              
     21 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/2                                                                                                                                  
     22 root      rt   0       0      0      0 S   0.0   0.0   0:02.21 watchdog/2                                                                                                                               
     23 root      rt   0       0      0      0 S   0.0   0.0   0:00.05 migration/2                                                                                                                              
     24 root      20   0       0      0      0 S   0.0   0.0   0:03.36 ksoftirqd/2                                                                                                                              
     26 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/2:0H-events_highpri                                                                                                              
     27 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/3                                                                                                                                  
     28 root      rt   0       0      0      0 S   0.0   0.0   0:02.34 watchdog/3                                                                                                                               
     29 root      rt   0       0      0      0 S   0.0   0.0   0:00.05 migration/3     
5.2 查看该进程下占用高 cpu 的线程
top -H -p 18038

输出:

[root@ashen-friede mnt]# top -H -p 18038
top - 13:48:40 up 50 days,  2:45,  2 users,  load average: 0.61, 0.92, 0.99
Threads:  56 total,   0 running,  56 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.6 us,  1.6 sy,  0.0 ni, 96.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7768.1 total,    641.1 free,   6309.3 used,    817.6 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   1190.8 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                  
  18038 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.00 java                                                                                                                                     
  18040 root      20   0 3804188 255812   9508 S   0.0   3.2   0:01.99 java                                                                                                                                     
  18041 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.67 java                                                                                                                                     
  18042 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.69 java                                                                                                                                     
  18043 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.68 java                                                                                                                                     
  18044 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.69 java                                                                                                                                     
  18045 root      20   0 3804188 255812   9508 S   0.0   3.2   1:09.25 java                                                                                                                                     
  18046 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.02 java                                                                                                                                     
  18047 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.02 java                                                                                                                                     
  18048 root      20   0 3804188 255812   9508 S   0.0   3.2   0:00.00 java                                                                                                                                     
  18049 root      20   0 3804188 255812   9508 S   0.0   3.2   0:19.32 java             

5.3 转换线程ID

printf "%x\n" 18040

输出:

[root@ashen-friede mnt]# printf "%x\n" 18040
4678

5.4 定位cpu占用线程

jstack 18038|grep 4678 -A 30

输出:

[root@ashen-friede mnt]# jstack 18038|grep 4678 -A 30
"Listener at 0.0.0.0/9867" #1 prio=5 os_prio=0 tid=0x00007f8f44014000 nid=0x4678 in Object.wait() [0x00007f8f4b922000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x0000000086f65320> (a java.lang.Thread)
	at java.lang.Thread.join(Thread.java:1252)
	- locked <0x0000000086f65320> (a java.lang.Thread)
	at java.lang.Thread.join(Thread.java:1326)
	at org.apache.hadoop.hdfs.server.datanode.BPServiceActor.join(BPServiceActor.java:583)
	at org.apache.hadoop.hdfs.server.datanode.BPOfferService.join(BPOfferService.java:358)
	at org.apache.hadoop.hdfs.server.datanode.BlockPoolManager.joinAll(BlockPoolManager.java:143)
	at org.apache.hadoop.hdfs.server.datanode.DataNode.join(DataNode.java:2773)
	at org.apache.hadoop.hdfs.server.datanode.DataNode.secureMain(DataNode.java:2909)
	at org.apache.hadoop.hdfs.server.datanode.DataNode.main(DataNode.java:2931)

"VM Thread" os_prio=0 tid=0x00007f8f44081800 nid=0x467d runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f8f44029800 nid=0x4679 runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f8f4402b000 nid=0x467a runnable 

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f8f4402d000 nid=0x467b runnable 

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f8f4402e800 nid=0x467c runnable 

"VM Periodic Task Thread" os_prio=0 tid=0x00007f8f440d3800 nid=0x4685 waiting on condition 

JNI global references: 387

五. jcmd

Jcmd 综合了前面的几个命令

说明
jcmd pid VM.uptime查看 JVM 的启动时长
jcmd pid GC.class_histogram查看 JVM 的类信息,这个可以查看每个类的实例数量和占用空间大小。
jcmd pid Thread.print查看 JVM 的Thread Dump
jcmd pid GC.heap_dump [file_name]查看 JVM 的Heap Dump,注意,如果只指定文件名,默认会生成在启动 JVM 的目录里。
jcmd pid VM.system_properties查看 JVM 的属性信息
jcmd pid VM.flags查看 JVM 的启动参数,注意,可以看到 -X 和 -XX 的参数信息
jcmd pid VM.command_line查看 JVM 的启动命令行
jcmd pid GC.run_finalization对 JVM 执行 java.lang.System.runFinalization(),尽量b别去调用这个对象的finalize方法。
jcmd pid GC.run对 JVM 执行 java.lang.System.gc(),告诉垃圾收集器打算进行垃圾收集,而垃圾收集器进不进行收集是不确定的
jcmd pid PerfCounter.print查看性能统计

六. jrunscript/jjs

1. 当 curl 命令用

jrunscript -e "cat('http://www.baidu.com')"

jrunscript -e "cat('http://8.142.187.127:8096/api-exchange/v/general_api/search_api?id=4eb726fc742d4bbda6476da1a869910f')"

2. 执行 js 脚本片段

jrunscript -e "print('hello, I am xiaohong')"

3. 执行 js 文件

function test(){
    console.log("Hello JS")
}

test()
jrunscript -l js -f /mnt/test.js

七. JVM 图形化工具

1. jconsole

在cmd命令行输入 jconsole 即可打开

  1. 本地 JVM 可以直接选择
  2. 远程 JVM 可以通过 JMX 方式连接
nohup java -Djava.rmi.server.hostname=172.29.38.64 -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=1507 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar api-exchange.jar &

8.142.187.127:1507

2. jvisualvm

3. jmc

4. idea 插件 VisualGC

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-03-17 22:14:33  更:2022-03-17 22:17:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/16 18:01:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码