OutOfMemoryError in Java Explained |Video upload date:  · Duration: PT8M13S  · Language: EN

Understand OutOfMemoryError in Java why it happens how to diagnose memory leaks and how to fix or tune the JVM for stable apps

What it means when your JVM screams OutOfMemoryError

OutOfMemoryError is the JVM's dramatic way of saying it cannot hand out more heap memory. That means a requested allocation failed and the process is stuck staring into the void. It can be a real memory leak or just a workload that needs more headroom. Either way the symptoms are the same and the on call engineer gets a rude wake up call.

Usual suspects that cause heap exhaustion

  • Long lived collections or listener lists that keep strong references to objects
  • A too small maximum heap setting via -Xmx for the real workload
  • Large temporary buffers or arrays during batch processing
  • Native memory pressure from direct byte buffers or JNI code
  • Metaspace growth from excessive class loading or leaking class loaders
  • Too many threads each with large stacks

How to diagnose without throwing darts at the console

Diagnosis follows two truths. First watch how the GC behaves. Second capture a heap image when the problem happens and read it like forensic evidence. Start with lightweight checks and then escalate to heap dumps and profilers.

Quick monitoring

  • Watch GC logs with jstat or your JVM flags to see frequent full GC cycles and rising used heap.
  • Check OS level memory to spot native pressure using top or ps or equivalent on your platform.
  • Use jcmd with the process id and ask for GC or native memory reports to get JVM side metrics.

Heap dump and analysis

When the crash occurs capture a heap dump. jcmd PID GC.heap_dump filename works reliably on modern JDKs. Then open the hprof in VisualVM or Eclipse MAT. In MAT look at the dominator tree and sort by retained size to find the biggest memory holders. Those are your prime suspects.

Profiling for allocation hotspots

Sampling profilers or allocation profilers will show which code paths create the most objects. That helps find short lived but very heavy allocation fronts or places where objects accumulate due to logic errors.

Practical fixes that actually stop the pager from vibrating

  • Increase heap with -Xmx if the workload legitimately needs more memory. This is a valid band aid but not a long term cure by itself.
  • Fix leaks by dropping unnecessary strong references, use weak references when appropriate, and clear caches on lifecycle boundaries.
  • Reduce cache sizes or switch to memory efficient collections for heavy data sets.
  • Tune garbage collector choices based on whether you want throughput or low pause times.
  • Investigate native memory and JNI or direct buffer handling when the heap looks fine but the process still runs out of memory.
  • Limit concurrent threads or reduce thread stack sizes when thread count is the problem.
  • Set a sensible MaxMetaspaceSize to avoid unbounded metaspace growth when class unloading is not keeping up.

Tips and troubleshooting checklist

  • Take the heap dump under real load for accurate results. The largest retained sets will point to the real memory hogs.
  • Use Eclipse MAT dominator tree to find the object graphs holding onto memory.
  • Use VisualVM or a commercial profiler to find allocation hotspots before they become leaks.
  • Prefer jcmd for production heap dumps when possible. It is less intrusive than some older tools.
  • When in doubt increase observability so you stop fixing symptoms and start fixing root causes.

If you like a predictable system and fewer 3 AM alarms then fix the leak not just the heap size. Extra RAM makes you feel better for a night but it does not stop bad memory habits. Now go capture a dump and find the culprit that is hoarding your objects like they are bitcoin.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.