2 Minutes To Code (β Version)

'java' command - 2 minute tutorial

How to run 'java' comamnd with option to trigger Heap dump on OouofMemoryError

If you java application leaks memory, then over time the memory availavle to the jvm as heap size gets exhausted. And then, your applocation crashes due to OutOfMemoryError.

One way to troubleshhot this is to get a jvm heap dump using jmap comamnd when the memory consumed by jvm is weel above the expected level. You can also get a jvm dump using jvisualvm tool .

In both these cases you manually trigger a heap dump. Howeevr, java comamnd provides oyu an option to request a heap dump whenever application throws an OutOfMemoryError and crashes.

The option for this is -XX:+HeapDumpOnOutOfMemoryError and the complete comand is java -XX:+HeapDumpOnOutOfMemoryError HelloWorld as shown in the image below. ( HelloWorld is the main class of the program.)

Now whenever an OutOfMemoryError is thrown, jvm dumps memory in a .hprof file in the current working directory. You can use Eclipse Memory Analyzer to analyze the heap dump.