1

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
javagc
  • 846
  • 1
  • 17
  • 37

3 Answers3

3

Use jmap:

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

refer to Java 7 jmap tutorial.

prime
  • 14,464
  • 14
  • 99
  • 131
harsh
  • 7,502
  • 3
  • 31
  • 32
  • jmap -dump:format=b,file=/tmp/heap.bin 3894 throws exeption 3894: well-known file is not secure – javagc Nov 25 '13 at 10:00
  • 2
    You should try changing the file location to somewhere accessible, see similar issue and resolution: http://stackoverflow.com/questions/9100149/jstack-well-known-file-is-not-secure – harsh Nov 25 '13 at 10:02
0

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

Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
0

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);
Mdhar9e
  • 1,376
  • 4
  • 23
  • 46