Take a look in documentation:
B.1.2 -XX:+HeapDumpOnOutOfMemoryError Option
The -XX:+HeapDumpOnOutOfMemoryError command-line option tells the
HotSpot VM to generate a heap dump when an allocation from the Java
heap or the permanent generation cannot be satisfied. There is no
overhead in running with this option, and so it can be useful for
production systems where OutOfMemoryError takes a long time to
surface.
You can also specify this option at runtime with the MBeans tab in the
jconsole utility.
The heap dump is in HPROF binary format, and so it can be analyzed
using any tools that can import this format. For example, the jhat
tool can be used to do rudimentary analysis of the dump.
-XX:+HeapDumpOnOutOfMemoryError flag does not introduce performance or security problems in run time. The flag is checked only after OutOfMemoryError has happened.
You can specify the actual path to which the file is saved using the corresponding -XX:HeapDumpPath flag. (Regardless of where the file is saved, make sure the filesystem and/or the Java process has the necessary permission configuration to be able to write there.)