Health Java heap memory dump

In dealing with the issue of stress tests, frequently encountered situation OOM, this time we need to record real-time situation of memory, usually play a dump file, and then use the MAT and other memory analysis tool to see which objects have been taking up a lot of memory , in the final analysis the local code requires optimization.

So how java dump file to play it?

java provides jmap command, as follows:

jmap -dump:format=b,file=/path/heap.bin 进程ID
jmap -dump:live,format=b,file=/path/heap.bin 进程ID
live参数:

Means that we need to grab the current memory objects in the life cycle, meaning that GC close objects do not go, then our most cases, you need to see is that these memory. And it will reduce the size of the dump file.

Can be run directly from the command line, of course, can also be performed using java as specified, for example:

package com.szh;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class TestDump {
public static void main(String[] args) throws InterruptedException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Runtime runtime = Runtime.getRuntime();

Scanner scanner = new Scanner(System.in);
System.out.println("请输入java进程PID:");
int pID = scanner.nextInt();
System.out.println("请输入dump文件输出路径:");
String dumpPath = scanner.next();
scanner.close();

if (!dumpPath.endsWith(File.separator)) {
dumpPath = dumpPath + File.separator;
}
String cmd = "jmap -dump,format=b,file=" + dumpPath + "_" + sdf.format(new Date()) + ".bin " + pID;
runtime.exec(cmd);
TimeUnit.SECONDS.sleep(5L);

while (true) {
cmd = "jmap -dump:live,format=b,file=" + dumpPath + "_" + sdf.format(new Date()) + ".bin " + pID;
Process process = runtime.exec(cmd);
/*InputStreamReader isr = new InputStreamReader(process.getInputStream(), "GBK");
BufferedReader br = new BufferedReader(isr);
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
System.out.println (STR);
} * /
TimeUnit.HOURS.sleep (1L);
}
}
}
 
----------------
description link: https: // blog. csdn.net/songzehao/article/details/84575834

 

Guess you like

Origin www.cnblogs.com/xd502djj/p/12176568.html