Domanda

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?

È stato utile?

Soluzione

Use jmap:

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

refer to Java 7 jmap tutorial.

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top