JDK性能分析与故障处理-命令行
一、命令演示
登录主机:21
docker ps -a | grep 'hub.ecs.com:6999/open_pro.*open-pro-apple2'
docker exec -it ID /bin/bash
jps
jinfo 1
jinfo 1 | grep 'sun.java.command' //程序参数 main方法入参
jinfo 1 | grep 'Command line' //虚拟机系统参数,全局参数
jstat -gc 1 3s 10
jstat -gcutil 1 3s 10
#简单看下后续有实例分析
登录测试环境主机:60
docker ps -a | grep 'test.com:6999/open_2k'
docker exec -it ID /bin/bash
jstack 1 > stack1.txt
cat stack1.txt | grep 'java.lang.Thread.State'
cat stack1.txt | grep 'java.lang.Thread.State'| wc -l
cat stack1.txt | grep 'java.lang.Thread.State'|sort| uniq
线程状态:https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html
New、Runnable(Runnable/Running)、Blocked(Blocked/WAITING/TIMED_WAITING)、Dead
#需要工具分析,肉眼不可读
jmap -dump:format=b,file=dump2019.dump 1
二、实例分析(代码见分割线下方)
1.BigObject
VM Options:-Xmx30M -Xms30M
1.1命令行观察内存变化
jstat -gcutil 1 1s 200
1.2visualVM观察
2.SupperBigObject
VM Options:-Xmx30M -Xms30M
1.1命令行观察内存变化
jstat -gcutil 1 1s 200
jstat -gc 1 1s 200
1.2visualVM观察
3.HighCPU(分析stack)
3.1 jstack或visualVM查看栈快照
jstack 1 > HighCPU.txt
本地方便则使用visualVM更方便
3.2 top命令
a."top"命令查找cpu高的进程pid
b."top -Hp pid" 查找cpu高的线程
(mac )
java -jar jvm-1.0-jar-with-dependencies.jar
9551-9584
jstack 9551 > HighCPU.txt
9584->0x2570
grep '0x2570' HighCPU.txt
4.OutOffMem(分析dump)
jmap或visualVM查看栈快照
jmap -dump:format=b,file=dump2019.dump pid
----分割线----以下是练习的代码---
package zero.desk.jdkcommand;
import java.util.ArrayList;
import java.util.List;
/**
* @author Zero
* @since 2019-09-21.
* Description:
* VM Options:-Xmx30M -Xms30M
*/
public class BigObject {
public Object instance = null;
private static final int ONE_MB = 1024 * 1024;
private byte[] bigSize = new byte[2 * ONE_MB];
public static void main(String[] args) throws InterruptedException {
Thread.sleep(12000);
for(int i=0;i<100;i++){
Thread.sleep(1000);
testGC();
}
}
private static void testGC() {
BigObject objA = new BigObject();
BigObject objB = new BigObject();
objA.instance = objB;
objB.instance = objA;
// System.gc();
}
}
//----
package zero.desk.jdkcommand;
/**
* @author Zero
* @since 2019-09-21.
* Description:
*/
public class HighCPU {
public static void main(String[] args) {
new Thread(() -> {
double d = 2.3;
while (true) {
long i = 1;
d = d * d;
if (i % 100000 == 0) {
System.out.println(i);
}
}
}).start();
new Thread(() -> {
long l = 1;
while (true) {
l++;
if (l % 100 == 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
//---
package zero.desk.jdkcommand;
import java.util.ArrayList;
import java.util.List;
/**
* @author Zero
* @since 2019-09-21.
* Description:
* VM Options:-Xmx50M -Xms50M
*/
public class OutOffMem {
private static List<BigObject> GCRoot = new ArrayList<>();
public static void main(String[] args) throws InterruptedException {
Thread.sleep(12000);
for(int i=0;i<100;i++){
Thread.sleep(1000);
testGC();
}
}
private static void testGC() {
BigObject objA = new BigObject();
BigObject objB = new BigObject();
objA.instance = objB;
objB.instance = objA;
//内存不会释放
GCRoot.add(objA);
GCRoot.add(objB);
System.gc();
}
}
//----
package zero.desk.jdkcommand;
/**
* @author Zero
* @since 2019-09-21.
* Description:
* VM Options:-Xmx30M -Xms30M
* 超过survivor的直接进入eden
* 超过年轻代的直接进入老年代
*/
public class SupperBigObject {
public Object instance = null;
private static final int ONE_MB = 1024 * 1024;
// private byte[] bigSize = new byte[8 * ONE_MB];
private byte[] bigSize = new byte[9 * ONE_MB];
public static void main(String[] args) throws InterruptedException {
Thread.sleep(12000);
for(int i=0;i<100;i++){
Thread.sleep(1000);
testGC();
}
}
private static void testGC() {
SupperBigObject objA = new SupperBigObject();
SupperBigObject objB = new SupperBigObject();
objA.instance = objB;
objB.instance = objA;
// System.gc();
}
}