I work with Java 1.7 and I want to print Heap Dump from java

...
Object heapDump=.... ;
...

System.out.println(heapDump);

Can anybody help me?

有帮助吗?

解决方案

Use jmap:

jmap -dump:format=b,file=heap.bin <pid>

refer to Java 7 jmap tutorial.

其他提示

You can use the HotSpotDiagnosticMXBean for creating a heap dump programmatically.

You can't print the Heap Dump directly using S.O.P Statement. But you can dump all the data in a file. JVM will create heap dump every time when your application throws an OutOfMemoryError. HeapDumpPath is used to set location of heap dumps.

We can also use jmap from our code. Assume name,pid are the fileds retrieving. To get a pid from code use we need to use java.lang.management.ManagementFactory.

String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.substring(0, name.indexOf("@"));
After that we can start jmap process like this:
String[] cmd = { "jmap", "-dump:file=D:\\temp\\heapdumps\\dump.bin", pid };
Process p = Runtime.getRuntime().exec(cmd);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top