JVM性能调优
- 1. 阿里巴巴Arthas详解
- 1.1 Arthas使用
本文是按照自己的理解进行笔记总结,如有不正确的地方,还望大佬多多指点纠正,勿喷。
课程内容:
1、阿里巴巴Arthas调优工具详解
2、GC日志详解与调优分析
3、Class常量池与运行时常量池详解
4、字符串常量池与基本类型常量池详解
1. 阿里巴巴Arthas详解
Arthas是 Alibaba在2018年9月开源的Java诊断工具。支持JDK6+,采用命令行交互模式,可以方便的定位和诊断线上程序运行问题。Arthas官方文档十分详细,详见: https://alibaba.github.io/arthas
1.1 Arthas使用
#github下载arthas
wget https://alibaba.github.io/arthas/arthas-boot.jar
#或者Gitee下载
wget https://arthas.gitee.io/arthas-boot.jar
用java -jar运行即可,可以识别机器上所有Java进程(我们这里之前已经运行了一个Arthas测试程序,代码见下方)
package ding;
import java.util.HashSet;
public class Arthas {
private static HashSet hashSet = new HashSet();
public static void main(String[] args) {
//模拟cpu过高
cpuHigh();
//模拟线程死锁
deadThread();
//不断地向HashSet集合增加数据
addHashSetThread();
}
/**
* 不断地向hashSet集合添加数据
*/
public static void addHashSetThread(){
//初始化常量
new Thread(()->{
int count = 0;
while (true){
try {
hashSet.add("count" + count);
Thread.sleep(10000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static void cpuHigh() {
new Thread(()->{
while (true){
}
}).start();
}
/**
* 死锁
*/
private static void deadThread() {
//创建资源
Object resourceA = new Object();
Object resourceB = new Object();
//创建线程
Thread threadA = new Thread(()->{
synchronized (resourceA){
System.out.println(Thread.currentThread()+"get ResourceA");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+"waiting get ResourceB");
synchronized (resourceB){
System.out.println(Thread.currentThread()+"get ResourceB");
}
}
});
Thread threadB = new Thread(()->{
synchronized (resourceB){
System.out.println(Thread.currentThread()+"get ResourceB");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+"waiting get ResourceA");
synchronized (resourceB){
System.out.println(Thread.currentThread()+"get ResourceA");
}
}
});
threadA.start();
threadB.start();
}
}