package com.msb;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @description:
* @date : 2020/8/12 21:12
* @author: zwz
*/
public class T15_FullGC_Problem01 {
private static class CardInfo {
BigDecimal price = new BigDecimal(0.0);
String name = "张三";
int age = 5;
Date birthdate = new Date();
public void m() {
}
}
private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50,
new ThreadPoolExecutor.DiscardOldestPolicy());
public static void main(String[] args) throws Exception {
executor.setMaximumPoolSize(50);
for (; ; ) {
modelFit();
Thread.sleep(100);
}
}
private static void modelFit() {
List<CardInfo> taskList = getAllCardInfo();
taskList.forEach(info -> {
// do something
executor.scheduleWithFixedDelay(() -> {
//do sth with info
info.m();
}, 2, 3, TimeUnit.SECONDS);
});
}
private static List<CardInfo> getAllCardInfo() {
List<CardInfo> taskList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
CardInfo ci = new CardInfo();
taskList.add(ci);
}
return taskList;
}
}
GC日志
1. JPS定位到PID
2. jstack
jstack 定位线程状况,重点关注:WAITING BLOCKED
eg.
waiting on <0x0000000088ca3310> (a java.lang.Object)
假如有一个进程中100个线程,很多线程都在waiting on <xx> ,一定要找到是哪个线程持有这把锁
怎么找?搜索jstack dump的信息,找<xx> ,看哪个线程持有这把锁RUNNABLE
死锁检测
package com.lg;
/**
* @description: 互斥条件、请求与保持条件、不剥夺条件、循环等待条件
* @date : 2020/7/27 11:31
* @author: zwz
*/
public class MustDeadLock implements Runnable {
public int flag;
static final Object o1 = new Object();
static final Object o2 = new Object();
@Override
public void run() {
System.out.println("线程" + Thread.currentThread().getName() + "的flag为" + flag);
if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("线程1获得了两把锁");
}
}
}
if (flag == 2) {
synchronized (o2) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1) {
System.out.println("线程2获得了两把锁");
}
}
}
}
public static void main(String[] args) {
MustDeadLock lock = new MustDeadLock();
MustDeadLock lock2 = new MustDeadLock();
lock.flag = 1;
lock2.flag = 2;
Thread t1 = new Thread(lock, "t1");
Thread t2 = new Thread(lock2, "t2");
t1.start();
t2.start();
}
}
3 jinfo PID
4. arthas
-
JVM
-
确定jar包加载路径
-
代码反编译
-
线上debug:watch
watch demo.MathGame primeFactors "{params,target,returnObj,throwExp}" -x 2 -n 2
-
sm 查找方法
-
sc 查找类
-
readline 重新写java并编译成class。运行时替换class
- 原理:
字节码 AOP spring aop jvm级别 java agent 热部署jrebel
jmap 查找有多少对象产生
- 线上系统,内存特别大,jmap执行期间会对进程产生很大影响,甚至卡顿(电商不适合)
1:设定了参数HeapDump,OOM的时候会自动产生堆转储文件(不是很专业,因为多有监控,内存增长就会报警)
2:<font color='red'>很多服务器备份(高可用),停掉这台服务器对其他服务器不影响</font>
3:在线定位(一般小点儿公司用不到)
4:在测试环境中压测(产生类似内存增长问题,在堆还不是很大的时候进行转储) -
堆转储文件
网友评论