一. 常用指令
jps
查询正在运行的进程
jnc-job-product-75bfb467f8-9tqkm[/app]#jps
36375 Jps
510 JournalJobApplication
jnc-job-product-75bfb467f8-9tqkm[/app]#
top
- top
展示当前所有进程的CPU使用情况, - top -p pid
展示某个进程的cpu使用情况.
同时可以是输入shift + h, 可以展示详细的使用情况
jinfo
- jinfo -flags pid
打印当前jvm的参数
jnc-job-product-75bfb467f8-9tqkm[/app]#jinfo -flags 510
Attaching to process ID 510, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.212-b04
Non-default VM flags: -XX:CICompilerCount=3 -XX:CMSInitiatingOccupancyFraction=70 -XX:CompressedClassSpaceSize=528482304 -XX:GCLogFileSize=52428800 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=null -XX:InitialHeapSize=2147483648 -XX:InitialRAMPercentage=null -XX:LargePageSizeInBytes=134217728 -XX:+ManagementServer -XX:MaxHeapSize=4294967296 -XX:MaxMetaspaceSize=536870912 -XX:MaxNewSize=348913664 -XX:MaxRAMPercentage=null -XX:MaxTenuringThreshold=6 -XX:MetaspaceSize=268435456 -XX:MinHeapDeltaBytes=196608 -XX:MinMetaspaceFreeRatio=50 -XX:MinRAMPercentage=null -XX:NativeMemoryTracking=null -XX:NewSize=348913664 -XX:NumberOfGCLogFiles=10 -XX:OldPLABSize=16 -XX:OldSize=1798569984 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:ThreadStackSize=256 -XX:-UseAdaptiveSizePolicy -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+UseContainerSupport -XX:+UseGCLogFileRotation -XX:+UseParNewGC
Command line: -XX:NativeMemoryTracking=detail -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xdebug -Xrunjdwp:transport=dt_socket,address=55555,server=y,suspend=n -XX:+UseContainerSupport -XX:-UseAdaptiveSizePolicy -XX:InitialRAMPercentage=40.0 -XX:MinRAMPercentage=40.0 -XX:MaxRAMPercentage=40.0 -Xss256K -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=512M -XX:MinMetaspaceFreeRatio=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/zcore -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/tmp/log/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=50M -Dspring.session.redisSerializer=json -Dspring.session.redis.serializer=json -Djava.security.krb5.conf=/etc/krb5.conf -Xms2g -Xmx4g -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -XX:+UseConcMarkSweepGC -XX:LargePageSizeInBytes=128m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseContainerSupport -XX:-UseAdaptiveSizePolicy -XX:InitialRAMPercentage=40.0 -XX:MinRAMPercentage=40.0 -XX:MaxRAMPercentage=40.0 -Xss256K -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=512M -XX:MinMetaspaceFreeRatio=50 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/zcore -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/tmp/log/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=50M -DaNuId= -DZSMART_HOME=/root/journal-job/journal-job-boot -Dz_app=journal-job -Dz_mode=classic -Dspring.config.additional-location=/root/journal-job/journal-job-boot/conf
jnc-job-product-75bfb467f8-9tqkm[/app]#
- jinfo -sysprops pid
查看java系统参数
jmap
- jmap -heap pid
打印堆的基本信息
jnc-job-product-75bfb467f8-9tqkm[/app]#jmap -heap 510
using parallel threads in the new generation.
using thread-local object allocation.
Concurrent Mark-Sweep GC
Heap Configuration:
MinHeapFreeRatio = 40 // 堆扩容比例, 堆空闲比例小于该值, 则需要扩容
MaxHeapFreeRatio = 70 // 堆缩容比例, 堆空闲比例大于该值, 则需要缩容
MaxHeapSize = 4294967296 (4096.0MB) // 堆大小最大值
NewSize = 348913664 (332.75MB) // 年轻代的大小
MaxNewSize = 348913664 (332.75MB) // 年轻代的最大值
OldSize = 1798569984 (1715.25MB) // 老年代的大小
NewRatio = 2 // 年轻代和老年代的比例
SurvivorRatio = 8 // Eden和Survivor的比例
MetaspaceSize = 268435456 (256.0MB)
CompressedClassSpaceSize = 528482304 (504.0MB)
MaxMetaspaceSize = 536870912 (512.0MB)
G1HeapRegionSize = 0 (0.0MB)
- jmap -histo pid > a.txt
查看类的实例个数, 一般都输入到某个文件中 - jmap -dump:format=b,file=euraka.hprof pid
导出dump文件
也可以设置自动导出dump文件
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./ (路径)
jstack
- jstack pid
打印线程信息
jstat
- jstat -gc pid
打印gc信息
jnc-job-product-75bfb467f8-9tqkm[/app]#jstat -gc 510
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
34048.0 34048.0 0.0 2239.9 272640.0 140404.7 1756416.0 122167.1 128000.0 122766.9 14848.0 13962.2 43 16.182 0 0.000 16.182
jnc-job-product-75bfb467f8-9tqkm[/app]#
C: 表示总大小
U:表示使用量
S: Survivor
E: Eden
O: Old
M: Meta
YGC: 年轻代的垃圾回收次数
YGCT: 年轻代的垃圾回收时间
FGC: 老年代的垃圾回收次数
FGCT: 老年代的垃圾回收时间
- jstat -gccapacity pid
只展示年轻代,老年代,元空间的内存情况
用上面的命令, 检查CPU使用情况
- 先准备测试代码
package com.learn.jvm;
/**
* Description:
*
* @author xu.wei
* @version 1.0.0
* @date 2021/7/22
* @see com.learn.jvm
*/
public class TestCpu {
public static void main(String[] args) {
while (true) {
compute();
}
}
public static void compute() {
int a = 1;
int b = 2;
int c = (a + b) * 2;
}
}
将编译后的文件,上传到服务器
然后,运行
[root@CentOS miler]# java com.learn.jvm.TestCpu
-
jps找到对应的pid
-
top -p pid 命令打印CPU使用率最高的线程
输入top -p 7097, 然后输入H, 展示具体的线程信息
-
printf "%x\n" 线程id抓换成16进制的id
由于线程id是十六进制的, 所以要将十进制的id转为十六进制.
-
jstack pid | grep -A 10 转换的id
查看堆栈信息, 找到对应的业务代码
从堆栈信息可以明显看到TestCpu第15行代码有问题,
Arthas
Arthas是alibba开源的java诊断工具. 接下来我们了解下Arthas的基本使用
- 下载arthas
下载arthas, 并启动arthas-boot.jar
[root@CentOS Downloads]# curl -O https://arthas.aliyun.com/arthas-boot.jar
[root@CentOS Downloads]# java -jar arthas-boot.jar
输出下列图形, 说明arthas启动成功
- 检查某个类是从哪个jar加载的
使用sc命令, 可以查看某个的加载情况, 包括从哪个jar包加载, 实现类是什么, 父类是什么, 加载器是什么
[arthas@3451]$ sc -d com.learn.LearnApplication
class-info com.learn.LearnApplication
code-source file:/root/Public/java-learn-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
name com.learn.LearnApplication
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name LearnApplication
modifier public
annotation org.springframework.boot.autoconfigure.SpringBootApplication
interfaces
super-class +-java.lang.Object
class-loader +-org.springframework.boot.loader.LaunchedURLClassLoader@7daf6ecc
+-sun.misc.Launcher$AppClassLoader@70dea4e
+-sun.misc.Launcher$ExtClassLoader@48ae9b55
classLoaderHash 7daf6ecc
class-info com.learn.LearnApplication$$EnhancerBySpringCGLIB$$3585b56d
code-source file:/root/Public/java-learn-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
name com.learn.LearnApplication$$EnhancerBySpringCGLIB$$3585b56d
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name LearnApplication$$EnhancerBySpringCGLIB$$3585b56d
modifier public
annotation
interfaces org.springframework.context.annotation.ConfigurationClassEnhancer$EnhancedConfiguration
super-class +-com.learn.LearnApplication
+-java.lang.Object
class-loader +-org.springframework.boot.loader.LaunchedURLClassLoader@7daf6ecc
+-sun.misc.Launcher$AppClassLoader@70dea4e
+-sun.misc.Launcher$ExtClassLoader@48ae9b55
classLoaderHash 7daf6ecc
Affect(row-cnt:2) cost in 67 ms.
[arthas@3451]$
- 查看线上源码
使用jad命令查看线上源码, 可以用来确认修改的代码是否部署到环境上.
[arthas@3451]$ jad com.learn.LearnApplication
ClassLoader:
+-org.springframework.boot.loader.LaunchedURLClassLoader@7daf6ecc
+-sun.misc.Launcher$AppClassLoader@70dea4e
+-sun.misc.Launcher$ExtClassLoader@48ae9b55
Location:
file:/root/Public/java-learn-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
/*
* Decompiled with CFR.
*
* Could not load the following classes:
* org.springframework.boot.SpringApplication
* org.springframework.boot.autoconfigure.SpringBootApplication
*/
package com.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LearnApplication {
public static void main(String[] args) {
/*17*/ SpringApplication.run(LearnApplication.class, (String[])args);
}
}
Affect(row-cnt:2) cost in 854 ms.
[arthas@3451]$
- 检查某个方法的耗时
使用trace命令打印某个方法的耗时
trance com.learn.LearnApplication main
- watch命令
watch命令定义了4个观察点: -b:方法前; -e:异常前; -f:方法结束后 ; -s:方法返回后, 其中-f默认打开
[arthas@3451]$ watch -help
USAGE:
watch [-b] [-e] [--exclude-class-pattern ] [-x ] [-f] [-h] [-n ] [--listenerId ] [-E] [-M ] [-s] [-v] class-pattern method-pattern [express] [conditio
n-express]
SUMMARY:
Display the input/output parameter, return object, and thrown exception of specified method invocation
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params : the parameters array of method
params[0..n] : the element of parameters array
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
Examples:
watch -b org.apache.commons.lang.StringUtils isBlank params
watch -f org.apache.commons.lang.StringUtils isBlank returnObj
watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2
watch -bf *StringUtils isBlank params
watch *StringUtils isBlank params[0]
watch *StringUtils isBlank params[0] params[0].length==1
watch *StringUtils isBlank params '#cost>100'
watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]
watch javax.servlet.Filter * --exclude-class-pattern com.demo.TestFilter
WIKI:
https://arthas.aliyun.com/doc/watch
OPTIONS:
-b, --before Watch before invocation
-e, --exception Watch after throw exception
--exclude-class-pattern exclude class name pattern, use either '.' or '/' as separator
-x, --expand Expand level of object (1 by default)
-f, --finish Watch after invocation, enable by default
-h, --help this help
-n, --limits Threshold of execution times
--listenerId The special listenerId
-E, --regex Enable regular expression to match (wildcard matching by default)
-M, --sizeLimit Upper size limit in bytes for the result (10 * 1024 * 1024 by default)
-s, --success Watch after successful invocation
-v, --verbose Enables print verbose information, default value false.
The full qualified class name you want to watch
The method name you want to watch
the content you want to watch, written by ognl.
Examples:
params
params[0]
'params[0]+params[1]'
'{params[0], target, returnObj}'
returnObj
throwExp
target
clazz
method
Conditional expression in ognl style, for example:
TRUE : 1==1
TRUE : true
FALSE : false
TRUE : 'params.length>=0'
FALSE : 1==2
'#cost>100'