javagc
javagc

Reputation: 876

JAVA Print Heap Dump

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?

Upvotes: 1

Views: 3856

Answers (3)

Mdhar9e
Mdhar9e

Reputation: 1376

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);

Upvotes: 0

harsh
harsh

Reputation: 7692

Use jmap:

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

refer to Java 7 jmap tutorial.

Upvotes: 3

Kai Sternad
Kai Sternad

Reputation: 22840

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

Upvotes: 0

Related Questions