【linux下Arthas学习与使用】
Arthas服务监控以及内存分析
阿尔萨斯(Y)(巫妖王N)
关于服务器模块性能监控
目前接触最多的性能监测工具
-
glowroot
-
arthas
关于arthas
安装
-- 下载jar包
wget https://alibaba.github.io/arthas/arthas-boot.jar
-- 启动arthas
java -jar arthas-boot.jar
-- 选择jar服务进行监控
-- 停止监控
stop
使用
arthas命令主要分为三类:System Info(系统),Class/Method (类/方法), Method invocation (方法调用)
不过总结起来无需分类,按照实用程度记录即可:
-
全局JVM运行时监控,CPU,线程,内存,堆栈信息等
-
接口无反应或调用耗时过长排查
-
CPU飙高排查
-
发版后没有理想的效果,怀疑发版所用是不是旧代码,,未pull成功等
-
线上可以进行热部署,无需重新发版
JVM监控信息:dashboard,thread等
1、进程:输入dashboard,
仪表盘持续打印当前进程相关信息
如下图,可以看到nonheap 和 metaspace(非堆),metaspace是java8新出现的概念
(堆)Heap分为3个区:
Young即新生代(分为Eden区、From Survivor、To Survivor三个区域,默认比例是8:1:1),
OldGen即老生代。
Young保存刚实例化的对象。当该区被填满时,GC会将对象移到Old区。
Permanent区则负责保存反射对象。
## 注意:java8的时候去除PermGen,将其中的方法区移到non-heap中的Metaspac,Metaspace与PermGen之间最大的区别在于:Metaspace并不在虚拟机内存中,而是使用本地内存
(非堆)MetaSpace:全称是MetaSpace,即方法区。用于存放Class和Meta信息,Class在被Load的时候被放入该区域。
## 去除PermGen的好处
将字符串常量池从PermGen分离出来,与类的元数据分开,提升类的元数据的独立性
将类的元数据从PermGen剥离出来到Metaspace,可以提升对元数据的管理同时提升GC效率
2、[thread 线程id] 查看占用CPU最高的线程,总线程数,跟线程状态对应的数量
## 查看具体线程信息使用 ,或者查看CPU使用率前N(N=3)的线程: thread -n 3

方法级别监控信息: tt、watch 、trace、stack、monitor等
1、tt
## 方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测,比如监控某类下的特定方法(selectList):
[arthas@7426]$ tt -t com.wang.library.api.controller.v1.FileManageController selectList
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 235 ms, listenerId: 2
INDEX TIMESTAMP COST(ms) IS-RET IS-EXP OBJECT CLASS METHOD
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1000 2022-09-11 17:48:50 158.319126 true false 0x44035c0e FileManageController selectList
1001 2022-09-11 17:49:09 108.755058 true false 0x44035c0e FileManageController selectList
2、watch
## watch className methodName '{params, returnObj,throwExp}' -n 5 -x 3
## watch 类所在路径 所检测方法名 '参数, 返回值, 异常信息(如果有的话)' 抓5次,参数层级(深度)设置为三级
## 例子:
[arthas@7426]$ watch com.wang.library.app.service.Impl.FileManageServiceImpl queryList '{params, returnObj,throwExp}' -n 5 -x 3
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 74 ms, listenerId: 3
method=com.wang.library.app.service.Impl.FileManageServiceImpl.queryList location=AtExit
ts=2022-09-12 10:44:45; [cost=304.375791ms] result=@ArrayList[
@Object[][isEmpty=true;size=0],
@Page[
@FileInfo[
FIELD_FILE_ID=@String[fileId],
FIELD_FILE_NAME=@String[fileName],
FIELD_FILE_TYPE=@String[fileType],
FIELD_FILE_Size=@String[fileSize],
FIELD_FILE_URL=@String[fileUrl],
FIELD_ATTRIBUTE1=@String[attribute1],
FIELD_ATTRIBUTE2=@String[attribute2],
FIELD_ATTRIBUTE3=@String[attribute3],
FIELD_ATTRIBUTE4=@String[attribute4],
FIELD_ATTRIBUTE5=@String[attribute5],
FIELD_USER_NAME=@String[userName],
fileId=@Long[104],
fileName=@String[视频类型测试.mp4],
fileType=@String[.mp4],
fileSize=@String[4359952],
fileUrl=@String[/zoey/upload/视频类型测试.mp4],
attribute1=@String[猫猫测试],
attribute2=@String[2022-07-31 11:00:26],
attribute3=null,
attribute4=null,
attribute5=null,
userName=@String[ljc],
],
@FileInfo[
FIELD_FILE_ID=@String[fileId],
FIELD_FILE_NAME=@String[fileName],
FIELD_FILE_TYPE=@String[fileType],
FIELD_FILE_Size=@String[fileSize],
FIELD_FILE_URL=@String[fileUrl],
FIELD_ATTRIBUTE1=@String[attribute1],
FIELD_ATTRIBUTE2=@String[attribute2],
FIELD_ATTRIBUTE3=@String[attribute3],
FIELD_ATTRIBUTE4=@String[attribute4],
FIELD_ATTRIBUTE5=@String[attribute5],
FIELD_USER_NAME=@String[userName],
fileId=@Long[106],
fileName=@String[31d51448bfb8c7c42eda05f6a2940d131e96ddc2bd76a20afcb78d34c28ede33.jpg],
fileType=@String[.jpg],
fileSize=@String[187697],
fileUrl=@String[/zoey/upload/31d51448bfb8c7c42eda05f6a2940d131e96ddc2bd76a20afcb78d34c28ede33.jpg],
attribute1=@String[],
attribute2=@String[2022-07-31 12:50:25],
attribute3=null,
attribute4=null,
attribute5=null,
userName=@String[ljc],
],
@FileInfo[
FIELD_FILE_ID=@String[fileId],
FIELD_FILE_NAME=@String[fileName],
FIELD_FILE_TYPE=@String[fileType],
FIELD_FILE_Size=@String[fileSize],
FIELD_FILE_URL=@String[fileUrl],
FIELD_ATTRIBUTE1=@String[attribute1],
FIELD_ATTRIBUTE2=@String[attribute2],
FIELD_ATTRIBUTE3=@String[attribute3],