Question

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?

Était-ce utile?

La solution

Use jmap:

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

refer to Java 7 jmap tutorial.

Autres conseils

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top