调试CPU负载过高

利用JDK自带工具调试

D:\test>java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)

D:\test>jps -lm
10112
19080 sun.tools.jps.Jps -lm
18028 zzc.learn.Main

https://blog.csdn.net/qq_26264237/article/details/116263159

利用阿里的arthas调试

运行arthas-boot.jar

D:\test>java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.5.0
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 10112
  [2]: 18028 zzc.learn.Main
2

显示所有线程的信息(CPU使用率高的线程排在前面)

[arthas@18028]$ thread
Threads Total: 29, NEW: 0, RUNNABLE: 8, BLOCKED: 0, WAITING: 4, TIMED_WAITING: 2, TERMINATED: 0, Internal threads: 15
ID        NAME                                                       GROUP                        PRIORITY            STATE               %CPU               DELTA_TIME          TIME                INTERRUPTED        DAEMON
1         main                                                       main                         5                   RUNNABLE            101.44             0.203               1:1.312             false              false
2         Reference Handler                                          system                       10                  WAITING             0.0                0.000               0:0.000             false              true
3         Finalizer                                                  system                       8                   WAITING             0.0                0.000               0:0.000             false              true
4         Signal Dispatcher                                          system                       9                   RUNNABLE            0.0                0.000               0:0.000             false              true
5         Attach Listener                                            system                       

查看线程堆栈 thread id

[arthas@18028]$ thread 1
"main" Id=1 RUNNABLE
    at java.io.RandomAccessFile.read0(Native Method)
    at java.io.RandomAccessFile.read(RandomAccessFile.java:337)
    at java.io.RandomAccessFile.readLine(RandomAccessFile.java:915)
    at zzc.learn.io.RandomAccessFileDemo.readLine(RandomAccessFileDemo.java:34)
    at zzc.learn.io.RandomAccessFileDemo.testRead(RandomAccessFileDemo.java:21)
    at zzc.learn.Main.main(Main.java:9)

定位到代码:
zzc.learn.io.RandomAccessFileDemo.readLine 第34行

使用jad反编译源码

直接看源码更方便,也可以通过这种方式对比源码和部署的代码有什么不同。

[arthas@18028]$ jad zzc.learn.io.RandomAccessFileDemo

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@73d16e93
  +-sun.misc.Launcher$ExtClassLoader@5772d040

Location:
/F:/myworkspace/zzc-learn/zzc.learn.nio/target/classes/

       /*
        * Decompiled with CFR.
        */
       package zzc.learn.io;

       import java.io.File;
       import java.io.IOException;
       import java.io.RandomAccessFile;
       import java.util.ArrayList;
       import java.util.List;
       import org.junit.Test;

       public class RandomAccessFileDemo {
           public static List<String> readLine(RandomAccessFile randomAccessFile, int lineNumber, long pos) throws IOException {
               String line;
/*29*/         randomAccessFile.seek(pos);
               ArrayList<String> lineList = new ArrayList<String>();
/*31*/         int i = 0;
               do {
/*34*/             line = randomAccessFile.readLine();
/*35*/             lineList.add(line);
               } while (++i < lineNumber && line != null);
/*40*/         return lineList;
           }

           @Test
           public void testRead() throws IOException {
               File file = new File("F:\\gbicc\\bigdata_home\\20200831\\irbs_fin_rept_subj.20200831.dat");
               RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
/*18*/         long filePointer = randomAccessFile.getFilePointer();
/*19*/         int i = 0;
               while (i < 31783966) {
/*20*/             long start = System.currentTimeMillis();
/*21*/             List<String> readLine = RandomAccessFileDemo.readLine(randomAccessFile, 50000, filePointer);
/*22*/             long end = System.currentTimeMillis();
/*23*/             filePointer = randomAccessFile.getFilePointer();
/*24*/             System.err.println("鑰楁椂锛?" + (end - start));
/*19*/             ++i;
               }
           }
       }

Affect(row-cnt:1) cost in 488 ms.

使用 tt 查看方法调用时的所有入参和返回值、抛出的异常、对象本身等数据

[arthas@7208]$ tt -t zzc.learn.io.RandomAccessFileDemo readLine -n 100
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 44 ms, listenerId: 1
 INDEX          TIMESTAMP                           COST(ms)           IS-RET        IS-EXP         OBJECT                     CLASS                                                  METHOD
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1000           2021-05-06 15:27:05                 3918.8451          true          false          NULL                       RandomAccessFileDemo                                   readLine
 1001           2021-05-06 15:27:09                 3996.0392          true          false          NULL                       RandomAccessFileDemo                                   readLine
 1002           2021-05-06 15:27:13                 4006.1736          true          false          NULL                       RandomAccessFileDemo                                   readLine
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26264237

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值