Java Fullgc 排查演练 memory analyzer

58 阅读1分钟

相关资料

1. 下载代码库用于模拟fullgc

模拟触发fullgc的代码库: gitee.com/YouYuangang…

public void MemoryOomSimulation(int taskNum, int taskMem /*MB*/, int taskDuration /*ms*/,
                                Integer taskDelay /*ms*/) {
    for (int i = 0; i < taskNum; i++) {
        Runnable task = () -> {
            try {
                byte[] needMem = new byte[taskMem * 1024 * 1024];
                Thread.sleep(taskDuration);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        queryExecutor.execute(task);
        if (Objects.nonNull(taskDelay) && taskDelay > 0) {
            try {
                Thread.sleep(taskDelay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2. 在idea启动模块 , 添加VM 参数(fullgc前自动dump)

-Xms初始堆大小, -Xmx最大堆大小,

-XX:+HeapDumpBeforeFullGC 表示fullgc前自动dump

dump文件路径和gc日志路径自己设置,记住,下面分析要用

-Xms128m -Xmx128m -XX:+HeapDumpBeforeFullGC -XX:HeapDumpPath=D:\03-projects\java_web_springBoot_test\springboot-web-demo -XX:+PrintGCDetails -Xloggc:/root/webdemo/gc.log

3. 触发fullgc,得到dump文件

云服务器可以用curl命令触发。

curl --location --request GET 'http://127.0.0.1:8888/mem100?taskNum=10&taskMem=10&taskDuration=5000&taskDelay=500' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)'

windows可以直接在浏览器输入http接口地址触发。

http://127.0.0.1:8888/mem100?taskNum=10&taskMem=10&taskDuration=5000&taskDelay=500

taskNum表示任务数量, taskMem表示一个任务占多少内存MB, taskDuration表示任务持续多久ms, taskDelay表示任务提交间隔ms。

4. memory analyzer分析dump文件