• JVM Optimization Learning(四)


    目录

    一、JVM Optimization

    1、JVM Optimization

    1、基础概念

    2、什么是调优?

    3、调优,从规划开始

    4、调优案例

    2、JVM Optimization Case

    jconsole远程连接

    jvisualvm远程连接

    jvisualvm导入dump文件进行分析


    一、JVM Optimization

    1、JVM Optimization

    1、基础概念

    吞吐量:用户代码执行时间 /(用户代码执行时间 + 垃圾回收时间)

    响应时间:STW越短,响应时间越好

            所谓调优,首先确定,追求啥?吞吐量优先,还是响应时间优先?还是在满足一定的响应时间的情况下,要求达到多大的吞吐量...

    吞吐量:科学计算、数据挖掘;吞吐量优先的一般:(PS+PO)

    响应时间:网站 GUI API(1.8 G1)

    2、什么是调优?

    1. 根据需求进行JVM规划和预调优

    2. 优化运行JVM运行环境(慢,卡顿)

    3. 解决JVM运行过程中出现的各种问题(OOM)

    3、调优,从规划开始

    调优,从业务场景开始,没有业务场景的调优都是耍流氓
    无监控(压力测试,能看到结果),不调优

    步骤:

    1. 熟悉业务场景(没有最好的垃圾回收器,只有最合适的垃圾回收器)

      1. 响应时间、停顿时间 [CMS G1 ZGC] (需要给用户作响应)

      2. 吞吐量 = 用户时间 /( 用户时间 + GC时间) [PS]

    2. 选择垃圾回收器组合

    3. 计算内存需求(经验值 1.5G 16G)

    4. 选定CPU(越高越好)

    5. 设定年代大小、升级年龄

    6. 设定日志参数

      1. -Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log -XX:+UseGCLogFileRotation
      2. -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M -XX:+PrintGCDetails
      3. -XX:+PrintGCDateStamps -XX:+PrintGCCause

            或者每天产生一个日志文件

            7.观察日志情况

    4、调优案例

    案例1:垂直电商,最高每日百万订单,处理订单系统需要什么样的服务器配置?

            1小时360000集中时间段, 100个订单/秒,(找一小时内的高峰期,1000订单/秒)

            计算:一个订单产生需要多少内存?512K * 1000 500M内存

    案例2:12306遭遇春节大规模抢票应该如何支撑?

            12306应该是中国并发量最大的秒杀网站:号称并发量100W最高
    CDN -> LVS -> NGINX -> 业务系统 -> 每台机器1W并发(10K问题) 100台机器
    普通电商订单 -> 下单 ->订单系统(IO)减库存 ->等待用户付款
    12306的一种可能的模型: 下单 -> 减库存 和 订单(redis kafka) 同时异步进行 ->等付款
    减库存最后还会把压力压到一台服务器
    可以做分布式本地库存 + 单独服务器做库存均衡
    大流量的处理方法:分而治之

    案例3:有一个50万PV的资料类网站(从磁盘提取文档到内存)原服务器32位,1.5G 的堆,用户反馈网站比较缓慢,因此公司决定升级,新的服务器为64位,16G 的堆内存,结果用户反馈卡顿十分严重,反而比以前效率更低了

    1. 为什么原网站慢?  很多用户浏览数据,很多数据load到内存,内存不足,频繁GC,STW长,响应时间变慢
    2. 为什么会更卡顿? 内存越大,FGC时间越长
    3. 怎么办? PS -> PN + CMS 或者 G1

    案例4:系统CPU经常100%,如何调优?(面试)

            CPU100%那么一定有线程在占用系统资源,
            1. 找出哪个进程cpu高(top)
            2. 该进程中的哪个线程cpu高(top -Hp)
            3. 导出该线程的堆栈 (jstack)
            4. 查找哪个方法(栈帧)消耗时间 (jstack)
            5. 工作线程占比高 | 垃圾回收线程占比高

    案例5:系统内存飙高,如何查找问题?(面试)

            1. 导出堆内存 (jmap)
            2. 分析 (jhat jvisualvm mat jprofiler ... )

    案例6:如何监控JVM

            1.jstat jvisualvm jprofiler arthas top...

    案例7:如果有一个系统,内存一直消耗不超过10%,但是观察GC日志,发现FGC总是频繁产生,会是什么引起的?

            代码中显式调用System.gc();

    2、JVM Optimization Case

    Linux安装JDK1.8

    1、测试程序

    1. package com.lwz.jvm;
    2. import java.math.BigDecimal;
    3. import java.util.ArrayList;
    4. import java.util.Date;
    5. import java.util.List;
    6. import java.util.concurrent.ScheduledThreadPoolExecutor;
    7. import java.util.concurrent.ThreadPoolExecutor;
    8. import java.util.concurrent.TimeUnit;
    9. /**
    10. * 从数据库中读取信用数据,套用模型,并把结果进行记录和传输
    11. */
    12. public class T15_FullGC_Problem01 {
    13. private static class CardInfo {
    14. BigDecimal price = new BigDecimal(0.0);
    15. String name = "张无忌";
    16. int age = 5;
    17. Date birthdate = new Date();
    18. public void m() {
    19. }
    20. }
    21. private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50,
    22. new ThreadPoolExecutor.DiscardOldestPolicy());
    23. public static void main(String[] args) throws Exception {
    24. executor.setMaximumPoolSize(50);
    25. for (;;){
    26. modelFit();
    27. Thread.sleep(100);
    28. }
    29. }
    30. private static void modelFit(){
    31. List taskList = getAllCardInfo();
    32. taskList.forEach(info -> {
    33. // do something
    34. executor.scheduleWithFixedDelay(() -> {
    35. //do sth with info
    36. info.m();
    37. }, 2, 3, TimeUnit.SECONDS);
    38. });
    39. }
    40. private static List getAllCardInfo(){
    41. List taskList = new ArrayList<>();
    42. for (int i = 0; i < 100; i++) {
    43. CardInfo ci = new CardInfo();
    44. taskList.add(ci);
    45. }
    46. return taskList;
    47. }
    48. }

    2、把这个程序拷贝到linux中带文件夹com/lwz/jvm/T15_FullGC_Problem01.java

    编译+执行,跑10-18分钟出现Full GC

    1. [root@localhost java]# ls
    2. arthas jdk1.8.0_202 jdk-21.0.1_linux-x64_bin.tar.gz
    3. com jdk-21.0.1 jdk-8u202-linux-x64.tar.gz
    4. [root@localhost java]# javac com/lwz/jvm/T15_FullGC_Problem01.java
    5. [root@localhost java]# java -Xms200M -Xmx200M -XX:+PrintGC com.lwz.jvm.T15_FullGC_Problem01
    6. [GC (Allocation Failure) 51712K->1112K(196608K), 0.0050199 secs]
    7. [GC (Allocation Failure) 52824K->23134K(196608K), 0.0461103 secs]
    8. [GC (Allocation Failure) 74846K->54592K(196608K), 0.0757565 secs]
    9. [GC (Allocation Failure) 106304K->90348K(196608K), 0.0851281 secs]
    10. [GC (Allocation Failure) 142060K->125204K(195584K), 0.0853713 secs]
    11. [Full GC (Ergonomics) 125204K->124016K(195584K), 0.7675142 secs]
    12. [Full GC (Ergonomics) 174704K->156146K(195584K), 0.4488494 secs]
    13. [Full GC (Ergonomics) 187391K->176629K(195584K), 0.3849072 secs]
    14. [Full GC (Ergonomics) 187391K->182317K(195584K), 0.4127277 secs]
    15. [Full GC (Ergonomics) 187391K->185019K(195584K), 0.3578134 secs]
    16. [Full GC (Ergonomics) 187391K->186352K(195584K), 0.4031946 secs]
    17. [Full GC (Ergonomics) 187183K->186540K(195584K), 0.3632300 secs]
    18. [Full GC (Allocation Failure) 186540K->186540K(195584K), 0.3203676 secs]
    19. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    20. at java.lang.reflect.Array.newArray(Native Method)
    21. at java.lang.reflect.Array.newInstance(Array.java:75)
    22. at java.util.Arrays.copyOf(Arrays.java:3212)
    23. at java.util.Arrays.copyOf(Arrays.java:3181)
    24. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.grow(ScheduledThreadPoolExecutor.java:921)
    25. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1014)
    26. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    27. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    28. at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:328)
    29. at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleWithFixedDelay(ScheduledThreadPoolExecutor.java:597)
    30. at com.lwz.jvm.T15_FullGC_Problem01.lambda$modelFit$1(T15_FullGC_Problem01.java:43)
    31. at com.lwz.jvm.T15_FullGC_Problem01$$Lambda$1/1418481495.accept(Unknown Source)
    32. at java.util.ArrayList.forEach(ArrayList.java:1257)
    33. at com.lwz.jvm.T15_FullGC_Problem01.modelFit(T15_FullGC_Problem01.java:41)
    34. at com.lwz.jvm.T15_FullGC_Problem01.main(T15_FullGC_Problem01.java:34)
    35. [Full GC (Ergonomics) 187104K->186536K(195584K), 0.3286528 secs]
    36. [Full GC (Allocation Failure) 186536K->186536K(195584K), 0.3276186 secs]
    37. [Full GC (Ergonomics) 186536K->186536K(195584K), 0.3817908 secs]
    38. [Full GC (Allocation Failure) 186536K->186536K(195584K), 0.3237639 secs]
    39. Exception in thread "pool-1-thread-41" java.lang.OutOfMemoryError: Java heap space
    40. [Full GC (Ergonomics) 186547K->186538K(195584K), 0.3503159 secs]
    41. [Full GC (Allocation Failure) 186538K->186538K(195584K), 0.3146940 secs]
    42. at java.lang.reflect.Array.newArray(Native Method)
    43. at java.lang.reflect.Array.newInstance(Array.java:75)
    44. at java.util.Arrays.copyOf(Arrays.java:3212)
    45. at java.util.Arrays.copyOf(Arrays.java:3181)
    46. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.grow(ScheduledThreadPoolExecutor.java:921)
    47. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1014)[Full GC (Ergonomics) 186553K->186540K(195584K), 0.3524864 secs]
    48. [Full GC (Allocation Failure) 186540K->186540K(195584K), 0.3175467 secs]
    49. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    50. [Full GC (Ergonomics) 186548K->186540K(195584K), 0.3627384 secs]
    51. [Full GC (Allocation Failure) 186540K->186540K(195584K), 0.3246809 secs]
    52. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    53. at java.util.concurrent.ScheduledThreadPoolExecutor.reExecutePeriodic(ScheduledThreadPoolExecutor.java:346)
    54. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:296)
    55. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    56. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    57. at java.lang.Thread.run(Thread.java:748)
    58. Exception in thread "pool-1-thread-47" java.lang.OutOfMemoryError: Java heap space
    59. Exception in thread "pool-1-thread-29" java.lang.OutOfMemoryError: Java heap space
    60. Exception in thread "pool-1-thread-10" java.lang.OutOfMemoryError: Java heap space
    61. Exception in thread "pool-1-thread-49" java.lang.OutOfMemoryError: Java heap space
    62. [Full GC (Ergonomics) 186587K->186534K(195584K), 0.3663284 secs]
    63. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3445732 secs]
    64. Exception in thread "pool-1-thread-43" java.lang.OutOfMemoryError: Java heap space
    65. [Full GC (Ergonomics) 186540K->186534K(195584K), 0.3350293 secs]
    66. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3302049 secs]
    67. Exception in thread "pool-1-thread-46" java.lang.OutOfMemoryError: Java heap space
    68. [Full GC (Ergonomics) 186540K->186534K(195584K), 0.3375518 secs]
    69. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3233926 secs]
    70. Exception in thread "pool-1-thread-11" java.lang.OutOfMemoryError: Java heap space
    71. [Full GC (Ergonomics) 186540K->186534K(195584K), 0.3457971 secs]
    72. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3213070 secs]
    73. Exception in thread "pool-1-thread-7" java.lang.OutOfMemoryError: Java heap space
    74. [Full GC (Ergonomics) 187361K->186534K(195584K), 0.3381650 secs]
    75. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3185285 secs]
    76. [Full GC (Ergonomics) 186541K->186534K(195584K), 0.3698434 secs]
    77. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3116594 secs]
    78. Exception in thread "pool-1-thread-21" java.lang.OutOfMemoryError: Java heap space
    79. [Full GC (Ergonomics) 186607K->186534K(195584K), 0.3356987 secs]
    80. [Full GC (Allocation Failure) 186534K->186534K(195584K), 0.3123781 secs]
    81. Exception in thread "pool-1-thread-1" java.lang.OutOfMemoryError: Java heap space
    82. Exception in thread "pool-1-thread-27" java.lang.OutOfMemoryError: Java heap space
    83. [Full GC (Ergonomics) 186540K->186533K(195584K), 0.3382997 secs]
    84. [Full GC (Allocation Failure) 186533K->186533K(195584K), 0.3269648 secs]
    85. Exception in thread "pool-1-thread-34" java.lang.OutOfMemoryError: Java heap space
    86. [Full GC (Ergonomics) 186853K->186533K(195584K), 0.3412933 secs]
    87. [Full GC (Allocation Failure) 186533K->186533K(195584K), 0.3179047 secs]
    88. [Full GC (Ergonomics) 186543K->186533K(195584K), 0.3286436 secs]
    89. [Full GC (Allocation Failure) 186533K->186533K(195584K), 0.3370087 secs]
    90. Exception in thread "pool-1-thread-5" java.lang.OutOfMemoryError: Java heap space
    91. Exception in thread "pool-1-thread-15" java.lang.OutOfMemoryError: Java heap space
    92. [Full GC (Ergonomics) 187382K->186532K(195584K), 0.3852196 secs]
    93. [Full GC (Ergonomics) 187071K->186532K(195584K), 0.3144036 secs]
    94. [Full GC (Allocation Failure) 186532K->186532K(195584K), 0.3237502 secs]
    95. Exception in thread "pool-1-thread-12" java.lang.OutOfMemoryError: Java heap space
    96. [Full GC (Ergonomics) 186567K->186532K(195584K), 0.3716423 secs]
    97. [Full GC (Allocation Failure) 186532K->186532K(195584K), 0.3273201 secs]
    98. Exception in thread "pool-1-thread-2" java.lang.OutOfMemoryError: Java heap space
    99. [Full GC (Ergonomics) 186660K->186532K(195584K), 0.3708459 secs]
    100. [Full GC (Allocation Failure) 186532K->186532K(195584K), 0.3180280 secs]
    101. Exception in thread "pool-1-thread-23" java.lang.OutOfMemoryError: Java heap space
    102. [Full GC (Ergonomics) 187380K->186532K(195584K), 0.3686135 secs]
    103. [Full GC (Ergonomics) 187378K->186532K(195584K), 0.3624713 secs]

    3、运维首先发现报警信息(CPU Memory)

    4、top命令观察到问题:内存不断增长CPU占用率居高不下

    再开一个窗口进行查看

    1. [root@localhost java]# top
    2. top - 23:34:29 up 1:07, 2 users, load average: 0.48, 0.54, 0.33
    3. Tasks: 126 total, 1 running, 125 sleeping, 0 stopped, 0 zombie
    4. %Cpu(s): 7.4 us, 0.5 sy, 0.0 ni, 92.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
    5. KiB Mem : 8008676 total, 6712688 free, 508676 used, 787312 buff/cache
    6. KiB Swap: 839676 total, 839676 free, 0 used. 7252628 avail Mem
    7. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    8. 1923 root 20 0 3783984 263912 12088 S 37.5 3.3 6:30.17 java
    9. 40 root 20 0 0 0 0 S 0.3 0.0 0:01.44 kworker/0:1
    10. 1 root 20 0 128144 6828 4160 S 0.0 0.1 0:01.75 systemd
    11. 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
    12. 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
    13. 6 root 20 0 0 0 0 S 0.0 0.0 0:00.03 ksoftirqd/0
    14. 7 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration/0
    15. 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
    16. 9 root 20 0 0 0 0 S 0.0 0.0 0:00.32 rcu_sched
    17. 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
    18. 11 root rt 0 0 0 0 S 0.0 0.0 0:00.04 watchdog/0
    19. 12 root rt 0 0 0 0 S 0.0 0.0 0:00.03 watchdog/1
    20. 13 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/1
    21. 14 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/1
    22. 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H

    5、top -Hp PID号   :把进程中所有线程打印出来,观察进程中的线程,哪个线程CPU和内存占比高、此例子比较平均

    1. [root@localhost java]# top -Hp 1923
    2. top - 23:36:41 up 1:09, 2 users, load average: 0.18, 0.41, 0.31
    3. Threads: 65 total, 0 running, 65 sleeping, 0 stopped, 0 zombie
    4. %Cpu(s): 7.7 us, 0.3 sy, 0.0 ni, 92.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
    5. KiB Mem : 8008676 total, 6712484 free, 508776 used, 787416 buff/cache
    6. KiB Swap: 839676 total, 839676 free, 0 used. 7252476 avail Mem
    7. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    8. 1950 root 20 0 3783984 263912 12088 S 2.0 3.3 0:07.06 java
    9. 1982 root 20 0 3783984 263912 12088 S 1.7 3.3 0:07.23 java
    10. 1957 root 20 0 3783984 263912 12088 S 1.3 3.3 0:07.20 java
    11. 1965 root 20 0 3783984 263912 12088 S 1.3 3.3 0:07.09 java
    12. 1974 root 20 0 3783984 263912 12088 S 1.3 3.3 0:07.22 java
    13. 1977 root 20 0 3783984 263912 12088 S 1.3 3.3 0:07.20 java
    14. 1940 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.30 java
    15. 1941 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.30 java
    16. 1954 root 20 0 3783984 263912 12088 S 1.0 3.3 0:06.88 java
    17. 1955 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.19 java
    18. 1967 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.21 java
    19. 1969 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.45 java
    20. 1973 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.18 java
    21. 1979 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.03 java
    22. 1981 root 20 0 3783984 263912 12088 S 1.0 3.3 0:07.27 java
    23. 2067 root 20 0 3783984 263912 12088 S 1.0 3.3 0:02.72 jav

    6、jstack  PID : 把进程下的线程打印出来和状态

    jstack定位线程状况,重点关注WAITING  BLOCKED

    eg. waiting on condition [0x00007f63236f5000]

    阿里规范规定,【强制】创建线程或线程池时请指定有意义的线程名称,方便出错时回溯,怎么样自定义线程池里的线程名称?(自定义ThreadFactory

    1. 正例:
    2. public class TimerTaskThread extends Thread{
    3. public TimerTaskThread(){
    4. super.setName("TimerTaskThread");
    5. //...
    6. }
    7. }
    1. [root@localhost ~]# jstack 1923
    2. 2023-11-28 23:51:52
    3. Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode):
    4. "Attach Listener" #78 daemon prio=9 os_prio=0 tid=0x00007f62f8001000 nid=0x89e waiting on condition [0x0000000000000000]
    5. java.lang.Thread.State: RUNNABLE
    6. "pool-1-thread-68" #77 prio=5 os_prio=0 tid=0x00007f62b4003800 nid=0x822 waiting on condition [0x00007f63236f5000]
    7. java.lang.Thread.State: WAITING (parking)
    8. at sun.misc.Unsafe.park(Native Method)
    9. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    10. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    11. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    12. at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
    13. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
    14. at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
    15. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1076)
    16. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    17. at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    18. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    19. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    20. at java.lang.Thread.run(Thread.java:748)
    21. "pool-1-thread-67" #76 prio=5 os_prio=0 tid=0x00007f62d8006800 nid=0x821 waiting on condition [0x00007f6322ceb000]
    22. java.lang.Thread.State: WAITING (parking)
    23. at sun.misc.Unsafe.park(Native Method)
    24. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    25. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    26. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    27. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
    28. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
    29. at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
    30. at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
    31. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1010)
    32. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    33. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    34. at java.util.concurrent.ScheduledThreadPoolExecutor.reExecutePeriodic(ScheduledThreadPoolExecutor.java:346)
    35. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:296)
    36. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    37. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    38. at java.lang.Thread.run(Thread.java:748)
    39. "pool-1-thread-66" #75 prio=5 os_prio=0 tid=0x00007f62b0003800 nid=0x820 runnable [0x00007f63229e8000]
    40. java.lang.Thread.State: WAITING (parking)
    41. at sun.misc.Unsafe.park(Native Method)
    42. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    43. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    44. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    45. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
    46. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
    47. at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
    48. at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
    49. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1010)
    50. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    51. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    52. at java.util.concurrent.ScheduledThreadPoolExecutor.reExecutePeriodic(ScheduledThreadPoolExecutor.java:346)
    53. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:296)
    54. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    55. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    56. at java.lang.Thread.run(Thread.java:748)
    57. "pool-1-thread-65" #74 prio=5 os_prio=0 tid=0x00007f62ac003800 nid=0x81f waiting on condition [0x00007f63237f6000]
    58. java.lang.Thread.State: WAITING (parking)
    59. at sun.misc.Unsafe.park(Native Method)
    60. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    61. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    62. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    63. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
    64. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
    65. at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
    66. at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
    67. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1010)
    68. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    69. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    70. at java.util.concurrent.ScheduledThreadPoolExecutor.reExecutePeriodic(ScheduledThreadPoolExecutor.java:346)
    71. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:296)
    72. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    73. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    74. at java.lang.Thread.run(Thread.java:748)
    75. "pool-1-thread-64" #73 prio=5 os_prio=0 tid=0x00007f62d4003800 nid=0x81e waiting on condition [0x00007f63216d5000]
    76. java.lang.Thread.State: WAITING (parking)
    77. at sun.misc.Unsafe.park(Native Method)
    78. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    79. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    80. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    81. at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
    82. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
    83. at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
    84. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1076)
    85. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    86. at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    87. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    88. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    89. at java.lang.Thread.run(Thread.java:748)
    90. "pool-1-thread-63" #72 prio=5 os_prio=0 tid=0x00007f62d8009000 nid=0x81d waiting on condition [0x00007f6321ddc000]
    91. java.lang.Thread.State: WAITING (parking)
    92. at sun.misc.Unsafe.park(Native Method)
    93. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    94. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    95. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    96. at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
    97. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
    98. at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
    99. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1076)
    100. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    101. at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    102. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    103. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    104. at java.lang.Thread.run(Thread.java:748)
    105. "pool-1-thread-62" #71 prio=5 os_prio=0 tid=0x00007f62c4003800 nid=0x81c waiting on condition [0x00007f6322eed000]
    106. java.lang.Thread.State: WAITING (parking)
    107. at sun.misc.Unsafe.park(Native Method)
    108. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    109. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    110. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    111. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
    112. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
    113. at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
    114. at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
    115. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1010)
    116. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    117. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    118. at java.util.concurrent.ScheduledThreadPoolExecutor.reExecutePeriodic(ScheduledThreadPoolExecutor.java:346)
    119. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:296)
    120. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    121. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    122. at java.lang.Thread.run(Thread.java:748)
    123. "pool-1-thread-61" #70 prio=5 os_prio=0 tid=0x00007f62e4002800 nid=0x81b waiting on condition [0x00007f63223e2000]
    124. java.lang.Thread.State: WAITING (parking)
    125. at sun.misc.Unsafe.park(Native Method)
    126. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    127. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    128. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    129. at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
    130. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
    131. at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
    132. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1076)
    133. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    134. at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    135. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    136. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    137. at java.lang.Thread.run(Thread.java:748)
    138. "pool-1-thread-60" #69 prio=5 os_prio=0 tid=0x00007f62ac002800 nid=0x81a waiting on condition [0x00007f63231f0000]
    139. java.lang.Thread.State: WAITING (parking)
    140. at sun.misc.Unsafe.park(Native Method)
    141. - parking to wait for <0x00000000faead268> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    142. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    143. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    144. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
    145. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
    146. at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
    147. at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
    148. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.offer(ScheduledThreadPoolExecutor.java:1010)
    149. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:1037)
    150. at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.add(ScheduledThreadPoolExecutor.java:809)
    151. at java.util.concurrent.ScheduledThreadPoolExecutor.reExecutePeriodic(ScheduledThreadPoolExecutor.java:346)
    152. at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:296)
    153. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    154. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    155. at java.lang.Thread.run(Thread.java:748)

    7、jps定位具体java进程

    1. [root@localhost ~]# jps
    2. 1923 T15_FullGC_Problem01
    3. 2251 Jps

    8、jinfo pid

    1. [root@localhost ~]# jinfo 1923
    2. Attaching to process ID 1923, please wait...
    3. Debugger attached successfully.
    4. Server compiler detected.
    5. JVM version is 25.202-b08
    6. Java System Properties:
    7. java.runtime.name = Java(TM) SE Runtime Environment
    8. java.vm.version = 25.202-b08
    9. sun.boot.library.path = /usr/java/jdk1.8.0_202/jre/lib/amd64
    10. java.vendor.url = http://java.oracle.com/
    11. java.vm.vendor = Oracle Corporation
    12. path.separator = :
    13. file.encoding.pkg = sun.io
    14. java.vm.name = Java HotSpot(TM) 64-Bit Server VM
    15. sun.os.patch.level = unknown
    16. sun.java.launcher = SUN_STANDARD
    17. user.country = US
    18. user.dir = /usr/java
    19. java.vm.specification.name = Java Virtual Machine Specification
    20. java.runtime.version = 1.8.0_202-b08
    21. java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
    22. os.arch = amd64
    23. java.endorsed.dirs = /usr/java/jdk1.8.0_202/jre/lib/endorsed
    24. java.io.tmpdir = /tmp
    25. line.separator =
    26. java.vm.specification.vendor = Oracle Corporation
    27. os.name = Linux
    28. sun.jnu.encoding = UTF-8
    29. java.library.path = /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    30. java.specification.name = Java Platform API Specification
    31. java.class.version = 52.0
    32. sun.management.compiler = HotSpot 64-Bit Tiered Compilers
    33. os.version = 3.10.0-1160.71.1.el7.x86_64
    34. user.home = /root
    35. user.timezone =
    36. java.awt.printerjob = sun.print.PSPrinterJob
    37. file.encoding = UTF-8
    38. java.specification.version = 1.8
    39. user.name = root
    40. java.class.path = .:/usr/java/jdk1.8.0_202/lib/dt.jar:/usr/java/jdk1.8.0_202/lib/tools.jar
    41. java.vm.specification.version = 1.8
    42. sun.arch.data.model = 64
    43. sun.java.command = com.lwz.jvm.T15_FullGC_Problem01
    44. java.home = /usr/java/jdk1.8.0_202/jre
    45. user.language = en
    46. java.specification.vendor = Oracle Corporation
    47. awt.toolkit = sun.awt.X11.XToolkit
    48. java.vm.info = mixed mode
    49. java.version = 1.8.0_202
    50. java.ext.dirs = /usr/java/jdk1.8.0_202/jre/lib/ext:/usr/java/packages/lib/ext
    51. sun.boot.class.path = /usr/java/jdk1.8.0_202/jre/lib/resources.jar:/usr/java/jdk1.8.0_202/jre/lib/rt.jar:/usr/java/jdk1.8.0_202/jre/lib/sunrsasign.jar:/usr/java/jdk1.8.0_202/jre/lib/jsse.jar:/usr/java/jdk1.8.0_202/jre/lib/jce.jar:/usr/java/jdk1.8.0_202/jre/lib/charsets.jar:/usr/java/jdk1.8.0_202/jre/lib/jfr.jar:/usr/java/jdk1.8.0_202/jre/classes
    52. java.vendor = Oracle Corporation
    53. file.separator = /
    54. java.vendor.url.bug = http://bugreport.sun.com/bugreport/
    55. sun.io.unicode.encoding = UnicodeLittle
    56. sun.cpu.endian = little
    57. sun.cpu.isalist =
    58. VM Flags:
    59. Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=209715200 -XX:MaxHeapSize=209715200 -XX:MaxNewSize=69730304 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=69730304 -XX:OldSize=139984896 -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
    60. Command line: -Xms200M -Xmx200M -XX:+PrintGC
    61. [root@localhost ~]#

    9、jstat -gc pid :打印出各种gc信息,不直观,不常用

    动态观察gc情况/阅读gc日志发现频繁gc/arthas观察/jconsole

    jstat -gc pid 500:每隔500个毫秒打印gc的情况

    1. [root@localhost ~]# jstat -gc 1923
    2. S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
    3. 8192.0 8192.0 0.0 0.0 50688.0 50519.7 136704.0 136422.5 4864.0 3888.8 512.0 420.0 5 0.264 81 31.082 31.345
    4. [root@localhost ~]#

    你是怎么定位OOM问题的?(面试)

            1、命令行  / arthas

            2、图形界面(jconsole、jvisualvm)用在什么地方?测试!

    10、jmap -histo pid | head -20 :查找有多少对象产生

    1. [root@localhost ~]# jmap -histo 1923 | head -20
    2. num #instances #bytes class name
    3. ----------------------------------------------
    4. 1: 898656 64703232 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask
    5. 2: 898682 35947280 java.math.BigDecimal
    6. 3: 898656 28756992 com.lwz.jvm.T15_FullGC_Problem01$CardInfo
    7. 4: 898656 21567744 java.util.Date
    8. 5: 898656 21567744 java.util.concurrent.Executors$RunnableAdapter
    9. 6: 898656 14378496 com.lwz.jvm.T15_FullGC_Problem01$$Lambda$2/1044036744
    10. 7: 1 3594640 [Ljava.util.concurrent.RunnableScheduledFuture;
    11. 8: 15697 502304 java.util.concurrent.locks.AbstractQueuedSynchronizer$Node
    12. 9: 1612 126808 [C
    13. 10: 717 82216 java.lang.Class
    14. 11: 532 45904 [I
    15. 12: 1601 38424 java.lang.String
    16. 13: 792 35920 [Ljava.lang.Object;
    17. 14: 10 25232 [B
    18. 15: 57 21432 java.lang.Thread
    19. 16: 188 10528 java.lang.invoke.MemberName
    20. 17: 276 8832 java.util.concurrent.ConcurrentHashMap$Node
    21. [root@localhost ~]#

    11、jmap -dump:format=b,file=xxx pid  :导出堆转储文件(线上内存大,不推荐)

    线上系统,内存特别大,jmap执行期间会对进程产生很大影响,甚至卡顿(电商不适合)

            1、设定了参数 -XX:+HeapDumpOnOutOfMemoryError,OOM的时候会自动产生转储文件

            2、很多服务器备份(高可用),停掉这台服务器对其他服务器不影响

            3、在线定位 arthas

    1. [root@localhost ~]# jmap -dump:live,file=a.log 1923
    2. Dumping heap to /root/a.log ...
    3. Heap dump file created

    12、java -Xms200M -Xmx200M -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError  com.lwz.jvm.T15_FullGC_Problem01

    13、使用MAT/jhat/jvisualvm 导入dump文件 进行dump文件分析

    1. [root@localhost ~]# jhat -J-Xmx512M a.log
    2. Reading from a.log...
    3. Dump file created Thu Nov 30 00:02:26 CST 2023
    4. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    5. at com.sun.tools.hat.internal.parser.HprofReader.readInstance(HprofReade r.java:742)
    6. at com.sun.tools.hat.internal.parser.HprofReader.readHeapDump(HprofReade r.java:491)
    7. at com.sun.tools.hat.internal.parser.HprofReader.read(HprofReader.java:2 38)
    8. at com.sun.tools.hat.internal.parser.Reader.readFile(Reader.java:92)
    9. at com.sun.tools.hat.Main.main(Main.java:159)
    10. [root@localhost ~]#

    java命令--jhat命令

    jhat命令:主要是用来分析java堆的命令,可以将堆中的对象以html的形式显示出来,包括对象的数量,大小等等,并支持对象查询语

    使用jmap等方法生成java的堆文件后,使用其进行分析。

    第一步:导出堆

    1. jmap -dump:live,file=a1.log pid
    2. [root@localhost java]# jmap -dump:live,file=a2.log 3362
    3. Dumping heap to /usr/java/a2.log ...
    4. Heap dump file created

    除了使用jmap命令,还可以通过以下方式:
    1、使用 jconsole 选项通过 HotSpotDiagnosticMXBean 从运行时获得堆转储(生成dump文件)
    2、虚拟机启动时如果指定了 -XX:+HeapDumpOnOutOfMemoryError 选项, 则在抛出 OutOfMemoryError 时, 会自动执行堆转储。
    3、使用 hprof 命令--(arthas heapdump文件)

    第二步:分析堆文件

    1. jhat -J-Xmx512M a1.log
    2. [root@localhost java]# jhat -J-Xmx512M a1.log
    3. Reading from a1.log...
    4. Dump file created Sun Dec 03 21:27:09 CST 2023
    5. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    6. at java.util.Hashtable.rehash(Hashtable.java:402)
    7. at java.util.Hashtable.addEntry(Hashtable.java:426)
    8. at java.util.Hashtable.put(Hashtable.java:477)
    9. at com.sun.tools.hat.internal.model.Snapshot.addHeapObject(Snapshot.java:166)
    10. at com.sun.tools.hat.internal.parser.HprofReader.readInstance(HprofReader.java:744)
    11. at com.sun.tools.hat.internal.parser.HprofReader.readHeapDump(HprofReader.java:491)
    12. at com.sun.tools.hat.internal.parser.HprofReader.read(HprofReader.java:238)
    13. at com.sun.tools.hat.internal.parser.Reader.readFile(Reader.java:92)
    14. at com.sun.tools.hat.Main.main(Main.java:159)
    15. [root@localhost java]#
    16. ====>重新导出一个小文件,执行下面命令
    17. [root@localhost java]# jhat -J-Xmx512M a2.log
    18. Reading from a2.log...
    19. Dump file created Sun Dec 03 21:49:05 CST 2023
    20. Snapshot read, resolving...
    21. Resolving 329332 objects...
    22. Chasing references, expect 65 dots.................................................................
    23. Eliminating duplicate references.................................................................
    24. Snapshot resolved.
    25. Started HTTP server on port 7000
    26. Server is ready.
    27. ==>解析Java堆转储文件,并启动一个 web server

    第三步:查看html

    http://ip:7000/

    对于jhat启动后显示的html页面中功能:
    (1)显示出堆中所包含的所有的类

    (2)显示平台包括的所有类的实例数量

    (3)堆实例的分布表

    (4)执行对象查询语句

    1. jhat中的OQL(对象查询语言)
    2. 如果需要根据某些条件来过滤或查询堆的对象,这是可能的,可以在jhat的html页面中执行OQL,来查询符合条件的对象
    3. 基本语法:
    4. select <javascript expression to select>
    5. [from [instanceof] <class name> <identifier>]
    6. [where <javascript boolean expression to filter>]
    7. 解释:
    8. (1)class name是java类的完全限定名,如:java.lang.String, java.util.ArrayList, [C是char数组, [Ljava.io.File是java.io.File[]
    9. (2)类的完全限定名不足以唯一的辨识一个类,因为不同的ClassLoader载入的相同的类,它们在jvm中是不同类型的
    10. (3)instanceof表示也查询某一个类的子类,如果不明确instanceof,则只精确查询class name指定的类
    11. (4)fromwhere子句都是可选的
    12. (5)java域表示:obj.field_name;java数组表示:array[index]
    13. 举例:
    14. 1)查询长度大于100的字符串
    15. select s from java.lang.String s where s.count > 100
    16. 2)查询长度大于256的数组
    17. select a from [I a where a.length > 256
    18. 3)显示匹配某一正则表达式的字符串
    19. select a.value.toString() from java.lang.String s where /java/(s.value.toString())
    20. 4)显示所有文件对象的文件路径
    21. select file.path.value.toString() from java.io.File file
    22. 5)显示所有ClassLoader的类名
    23. select classof(cl).name from instanceof java.lang.ClassLoader cl
    24. 6)通过引用查询对象
    25. select o from instanceof 0xd404d404 o
    26. built-in对象 -- heap
    27. (1)heap.findClass(class name) -- 找到类
    28. select heap.findClass("java.lang.String").superclass
    29. (2)heap.findObject(object id) -- 找到对象
    30. select heap.findObject("0xd404d404")
    31. (3)heap.classes -- 所有类的枚举
    32. select heap.classes
    33. (4)heap.objects -- 所有对象的枚举
    34. select heap.objects("java.lang.String")
    35. (5)heap.finalizables -- 等待垃圾收集的java对象的枚举
    36. (6)heap.livepaths -- 某一对象存活路径
    37. select heaplivepaths(s) from java.lang.String s
    38. (7)heap.roots -- 堆根集的枚举
    39. 辨识对象的函数
    40. (1)classof(class name) -- 返回java对象的类对象
    41. select classof(cl).name from instanceof java.lang.ClassLoader cl
    42. (2)identical(object1,object2) -- 返回是否两个对象是同一个实例
    43. select identical(heap.findClass("java.lang.String").name, heap.findClass("java.lang.String").name)
    44. (3)objectid(object) -- 返回对象的id
    45. select objectid(s) from java.lang.String s
    46. (4)reachables -- 返回可从对象可到达的对象
    47. select reachables(p) from java.util.Properties p -- 查询从Properties对象可到达的对象
    48. select reachables(u, "java.net.URL.handler") from java.net.URL u -- 查询从URL对象可到达的对象,但不包括从URL.handler可到达的对象
    49. (5)referrers(object) -- 返回引用某一对象的对象
    50. select referrers(s) from java.lang.String s where s.count > 100
    51. (6)referees(object) -- 返回某一对象引用的对象
    52. select referees(s) from java.lang.String s where s.count > 100
    53. (7)refers(object1,object2) -- 返回是否第一个对象引用第二个对象
    54. select refers(heap.findObject("0xd4d4d4d4"),heap.findObject("0xe4e4e4e4"))
    55. (8)root(object) -- 返回是否对象是根集的成员
    56. select root(heap.findObject("0xd4d4d4d4"))
    57. (9)sizeof(object) -- 返回对象的大小
    58. select sizeof(o) from [I o
    59. (10)toHtml(object) -- 返回对象的html格式
    60. select "" + toHtml(o) + "" from java.lang.Object o
    61. (11)选择多值
    62. select {name:t.name?t.name.toString():"null",thread:t} from instanceof java.lang.Thread t
    63. 数组、迭代器等函数
    64. (1)concat(enumeration1,enumeration2) -- 将数组或枚举进行连接
    65. select concat(referrers(p),referrers(p)) from java.util.Properties p
    66. (2)contains(array, expression) -- 数组中元素是否满足某表达式
    67. select p from java.util.Properties where contains(referres(p), "classof(it).name == 'java.lang.Class'")
    68. 返回由java.lang.Class引用的java.util.Properties对象
    69. built-in变量
    70. it -- 当前的迭代元素
    71. index -- 当前迭代元素的索引
    72. array -- 被迭代的数组
    73. (3)count(array, expression) -- 满足某一条件的元素的数量
    74. select count(heap.classes(), "/java.io./(it.name)")
    75. (4)filter(array, expression) -- 过滤出满足某一条件的元素
    76. select filter(heap.classes(), "/java.io./(it.name)")
    77. (5)length(array) -- 返回数组长度
    78. select length(heap.classes())
    79. (6)map(array,expression) -- 根据表达式对数组中的元素进行转换映射
    80. select map(heap.classes(),"index + '-->' + toHtml(it)")
    81. (7)max(array,expression) -- 最大值, min(array,expression)
    82. select max(heap.objects("java.lang.String"),"lhs.count>rhs.count")
    83. built-in变量
    84. lhs -- 左边元素
    85. rhs -- 右边元素
    86. (8)sort(array,expression) -- 排序
    87. select sort(heap.objects('[C'),'sizeof(lhs)-sizeof(rhs)')
    88. (9)sum(array,expression) -- 求和
    89. select sum(heap.objects('[C'),'sizeof(it)')
    90. (10)toArray(array) -- 返回数组
    91. (11)unique(array) -- 唯一化数组

    原文链接:https://www.cnblogs.com/baihuitestsoftware/articles/6406271.html

    14、找到代码的问题

    jconsole远程连接

    1、程序启动加入参数:起jmx协议

    1. java -Djava.rmi.server.hostname=192.168.18.70
    2. -Dcom.sun.management.jmxremote
    3. -Dcom.sun.management.jmxremote.port=11111
    4. -Dcom.sun.management.jmxremote.authenticate=false
    5. -Dcom.sun.management.jmxremote.ssl=false
    6. -Xms200M -Xmx200M -XX:+PrintGC com.lwz.jvm.T15_FullGC_Problem01

    注意:执行时,把上面命令整成一行执行

    2、关闭防火墙

    1. 关闭了linux的防火墙,才能在外面访问到对应的服务
    2. service firewalld status ;查看防火墙状态
    3. service firewalld stop:关闭防火墙

    3、windows上打开jconsole远程连接 192.168.18.70:11111

    点击不安全的连接

    jvisualvm远程连接

    文件--添加JMX连接

    监视

    内存中的对象占用内存比较大,问题很可能出现在这里

    jvisualvm导入dump文件进行分析

    导入a2.log

    可以分析dump文件

    Arthas(阿尔萨斯)--(一)

    JVM Optimization Learning(三)

    JVM Optimization Learning(五)

    再小的努力,乘以365都很明显!
    一个程序员最重要的能力是:写出高质量的代码!!
    有道无术,术尚可求也,有术无道,止于术。
    无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!

  • 相关阅读:
    [附源码]计算机毕业设计SpringBoot心理健康系统
    D. Corrupted Array
    【HarmonyOS】应用屏蔽截屏和录屏
    [CISCN2019 华北赛区 Day1 Web1]Dropbox 1
    java公交线路查询系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    基于51单片机电子钟闹钟12/24小时制LCD显示( proteus仿真+程序+设计报告+讲解视频)
    Go 语言数组基础教程 - 数组的声明、初始化和使用方法
    设计一个高效算法,将顺序表L的所有元素逆置,要求算法的空间复杂度为O(1)
    NLP模型笔记2022-18:GCN/GNN模型在nlp中的使用【论文+源码】
    Selenium 自动化 | 案例实战篇
  • 原文地址:https://blog.csdn.net/weixin_42472027/article/details/133563975