spring boot 模拟飚高代码

@Service
public class TestWhile
{
    /* 操作内存对象 */
    ConcurrentHashMap map = new ConcurrentHashMap();
    private void whileTrue(String threadName) {
        // 不设置退出条件,死循环
        while (true) {
            // 在死循环中不断的对map执行put操作,导致内存gc
            for (int i = 0; i <= 100000; i++) {
                map.put(Thread.currentThread().getName() + i, i);
            } // end for
        }// end while
    }
    @PostConstruct
    public void testWhile() {
        // 循环size,创建多线程,并发执行死循环
        for (int i = 0; i < 20; i++) {
            int finalI = i;
            // 新建并启动线程,调用whileTrue方法
            new Thread(() -> {
                whileTrue("李文-" + finalI);
            }).start();
        }
    }
}

top

 

使用  淘宝开源  show-busy-java-threads  快速排查

  1.  介绍:
    1. 作者 :淘宝 李鼎(哲良) oldratlee
    2. 用于快速排查Java的CPU性能问题(top us值过高),自动查出运行的Java进程中消耗CPU多的线程,并打印出其线程栈,从而确定导致性能问题的方法调用。
    3.  Git地址:https://github.com/oldratlee/useful-scripts

 执行:  curl -sLk 'https://raw.github.com/oldratlee/useful-scripts/release-2.x/bin/show-busy-java-threads' | bash -s -- -a 2.log  

 输出到 2.log 文件。

结果:GC引起的CPU 飚高, 【5】 引起GC的线程与执行代码方法。 可以定位到 whileTrue 方法有问题。

 

使用 淘宝开源 Arthas 排查问题

  1. 介绍: Arthas 是Alibaba开源的Java诊断工具 ,业界最强。
  2. GIT地址 : https://github.com/alibaba/arthas/blob/master/README_CN.md
  3. 不光是CPU线程问题排查,几乎可以包括所有问题的排查,在线反编译,动态热更新运行中的代码,在线请求链路跟踪等等功能。
curl -O https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar

选择需要排查的那个进程

1  + Enter

执行 dashboard  命令

初步可以判断为GC引发的CPU飚高

 

执行  thread -n 3 -i 5000  查看CPU使用率Top N线程的栈

结果: GC引起的CPU 飚高, 可以定位到线程运行链接方法 whileTrue  有问题。

推荐在服务出现问题, 执行以下  c  curl -sLk 'https://raw.github.com/oldratlee/useful-scripts/release-2.x/bin/show-busy-java-threads' | bash -s -- -a 2.log    然后在重启,这样重启后也会有写问题记录

12-17 11:25