3

I am a newbie on JAVA. I run a program on top of distributed framework implemented in JAVA.

When I use large data, the program crashes due to OutOfMemory with some error stacks. But the error stacks do not contain the information that I am looking for.

I want to check what kinds of data structures (java objects) were consuming the heap space at the time when it crashed.

Is there any well-known tricks, methods, or tools for it?

Thanks,

syko
  • 2,767
  • 3
  • 22
  • 40

1 Answers1

4

Use the HeapDumpOnOutOfMemoryError and HeapDumpPath options to generate a heap dump when an OutOfMemoryError occurs in the JVM.

For example:

$ java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/some/path MyApp

If you suspect a memory leak, you can use jmap, which is included with the JDK, to produce a heap dump of your application while it's running.

For example:

jmap -dump:live,format=b,file=dump.hprof <pid>

You can then analyze dump.hprof using an application such as YourKit to pinpoint what code is causing the leak.

References

  1. https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/clopts001.html

  2. https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr014.html

ck1
  • 4,487
  • 1
  • 15
  • 23
  • Thanks, which tool should I use the generated heap dump files? – syko Jul 19 '17 at 02:52
  • Use `jmap` as I described above. Note: the `live` option will cause a full GC cycle. If this is a production application and that worries you, remove this option. – ck1 Jul 19 '17 at 02:54