Sobes.tech
Senior

How to confirm out of memory in production and what to do about it? How to take a heap dump at the moment of OOM?

sobes.tech AI

Answer from AI

To confirm OutOfMemoryError (OOM) in production, the following signs can be observed:

  • Error messages like java.lang.OutOfMemoryError appear in JVM logs.
  • The application stops responding or crashes with memory errors.
  • Memory monitoring shows a sharp decrease in free heap memory.

What to do:

  1. Analyze logs and memory metrics.
  2. Take a heap dump at the moment of OOM for further analysis.
  3. Find memory leaks or objects that occupy too much space.
  4. Optimize code or increase heap size if justified.

How to take a heap dump during OOM:

  • Run JVM with the parameter -XX:+HeapDumpOnOutOfMemoryError, then a memory dump will be automatically created when OOM occurs.
  • You can specify the dump path using -XX:HeapDumpPath=/path/to/dump

Example of JVM startup:

java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/logs/heapdump.hprof -jar app.jar
  • If JVM is already running, you can use jmap or profiling tools to manually take a heap dump.

After obtaining the heap dump, analyze it using tools like Eclipse MAT to identify causes of leaks or excessive memory consumption.