OOM - 实战笔记

OOM - 实战

前期准备模拟OOM代码
public class TestJvm {
    static class OOMHeapDumpObject{
        String str ="1234567890";
    }

    public static void main(String[] args) throws InterruptedException {
        List<OOMHeapDumpObject> oomsAll = new ArrayList<OOMHeapDumpObject>();
        List<OOMHeapDumpObject> ooms = new ArrayList<OOMHeapDumpObject>();
        int count = 0;
        while (true) {
            ooms.add(new OOMHeapDumpObject());
            oomsAll.addAll(ooms);
            Thread.sleep(50);
        }
    }
}
启动参数增加OOM异常打印堆栈信息
-XX:+HeapDumpOnOutOfMemoryError 设置当首次遭遇内存溢出时导出此时堆中相关信息
-XX:HeapDumpPath=/tmp/heapdump.hprof 指定导出堆信息时的路径或文件名(默认当前目录)
java -jar -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
OOM异常输出 java_pid***.hprodf文件
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid16816.hprof ...
Heap dump file created [18570036 bytes in 0.027 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:267)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:241)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:233)
	at java.util.ArrayList.addAll(ArrayList.java:585)
	at com.TestJvm.main(TestJvm.java:21)
使用Java自带工具Java VisoualVM 打开java_pid***.hprodf文件 定位找OOM位置

在这里插入图片描述

在这里插入图片描述

使用Arthas - 常用命令

直接下载Arthas,java -jar启动,列出可监测的程序编号,直接输入编号回车
>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]: 26848 D:\soft\arthas-packaging-3.5.0-bin\arthas-core.jar
  [2]: 16796 com.TestJvm
 
> 2
 
[INFO] arthas home: D:\soft\arthas-packaging-3.5.0-bin
[INFO] Try to attach process 45116
[INFO] Found java home from System Env JAVA_HOME: C:\Program Files\Java\jdk1.8.0_281
[INFO] Attach process 45116 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'


wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.5.0
main_class
pid        45116
time       2021-04-26 14:34:12
dashboard 查看仪表盘
[arthas@23412]$ dashboard
ID   NAME                          GROUP          PRIORITY  STATE    %CPU      DELTA_TIM TIME      INTERRUPT DAEMON
-1   C1 CompilerThread3            -              -1        -        0.31      0.015     0:0.531   false     true
-1   VM Thread                     -              -1        -        0.31      0.015     0:0.265   false     true
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         5         RUNNABLE 0.0       0.000     0:0.000   false     true
13   arthas-timer                  system         5         WAITING  0.0       0.000     0:0.000   false     true
16   arthas-NettyHttpTelnetBootstr system         5         RUNNABLE 0.0       0.000     0:0.015   false     true
17   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.000   false     true
18   arthas-NettyWebsocketTtyBoots system         5         RUNNABLE 0.0       0.000     0:0.000   false     true
19   arthas-shell-server           system         5         TIMED_WA 0.0       0.000     0:0.000   false     true
Memory                    used    total    max     usage    GC
heap                      15M     18M      18M     83.67%   gc.ps_scavenge.count          30
ps_eden_space             1M      2M       2M      78.52%   gc.ps_scavenge.time(ms)       31
ps_survivor_space         0K      2048K    2048K   0.00%    gc.ps_marksweep.count         24
ps_old_gen                13M     13M      13M     97.02%   gc.ps_marksweep.time(ms)      561
nonheap                   29M     30M      -1      95.54%
code_cache                5M      6M       240M    2.37%
metaspace                 21M     21M      -1      97.29%
compressed_class_space    2M      2M       1024M   0.25%
Runtime
os.name                                                     Windows 10
os.version                                                  10.0
java.version                                                1.8.0_281
java.home                                                   C:\Program Files\Java\jdk1.8.0_281\jre
systemload.average                                          -1.00
processors                                                  12
timestamp/uptime                                            Mon Apr 26 14:37:33 CST 2021/119s
heapdump 输出当前堆栈快照,使用Java自带工具Java VisoualVM进行分析
Dumping heap to C:\Users\admin\AppData\Local\Temp\heapdump2021-04-26-14-408865243791871134538.hprof ...
Heap dump file created
thread 列出线程的信息
Threads Total: 37, NEW: 0, RUNNABLE: 10, BLOCKED: 0, WAITING: 4, TIMED_WAITING: 6, TERMINATED: 0, Internal threads: 17
ID    NAME                                 GROUP              PRIORITY     STATE       %CPU        DELTA_TIME   TIME        INTERRUPTED  DAEMON
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             5            RUNNABLE    0.0         0.000        0:0.171     false        true
13    RMI TCP Accept-0                     system             5            RUNNABLE    0.0         0.000        0:0.109     false        true
15    RMI Scheduler(0)                     system             5            TIMED_WAITI 0.0         0.000        0:0.000     false        true
22    arthas-timer                         system             5            WAITING     0.0         0.000        0:0.000     false        true
25    arthas-NettyHttpTelnetBootstrap-3-1  system             5            RUNNABLE    0.0         0.000        0:0.000     false        true
26    arthas-NettyWebsocketTtyBootstrap-4- system             5            RUNNABLE    0.0         0.000        0:0.000     false        true
27    arthas-NettyWebsocketTtyBootstrap-4- system             5            RUNNABLE    0.0         0.000        0:0.000     false        true
28    arthas-shell-server                  system             5            TIMED_WAITI 0.0         0.000        0:0.000     false        true
29    arthas-session-manager               system             5            TIMED_WAITI 0.0         0.000        0:0.000     false        true
30    arthas-UserStat                      system             5            WAITING     0.0         0.000        0:0.000     false        true
32    arthas-NettyHttpTelnetBootstrap-3-2  system             5            RUNNABLE    0.0         0.000        0:0.093     false        true
33    arthas-command-execute               system             5            RUNNABLE    0.0         0.000        0:0.078     false        true
1     main                                 main               5            TIMED_WAITI 0.0         0.000        0:0.093     false        false
thread 查看指定线程PID详情
[arthas@48568]$ thread 1
"main" Id=1 TIMED_WAITING
    at java.lang.Thread.sleep(Native Method)
    at com.TestJvm.main(TestJvm.java:22)
thread -n <数量> 查看指定条数线程详情
[arthas@48568]$ thread -n 2
"Reference Handler" Id=2 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.Reference$Lock@2ba7f086
    at java.lang.Object.wait(Native Method)
    -  waiting on java.lang.ref.Reference$Lock@2ba7f086
    at java.lang.Object.wait(Object.java:502)
    at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)


"Finalizer" Id=3 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.ReferenceQueue$Lock@37b860c4
    at java.lang.Object.wait(Native Method)
    -  waiting on java.lang.ref.ReferenceQueue$Lock@37b860c4
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值