Java Memory Leak

What is a Memory Leak in Java?

A memory leak occurs when objects are no longer needed but are still referenced, preventing the garbage collector from freeing up memory. This can lead to OutOfMemoryError and degraded performance.

How to Find Memory Leak Issues in Java

Java manages objects through a directed graph; if an object is unreachable, it can be garbage collected. During code review, pay attention to long-lifecycle objects: global collections (variables), use of singleton patterns, class static variables, etc. In Java implementation, also consider object release; the best method is to explicitly set the object to null when not in use. It’s best to follow the principle of “who creates, who releases”.

Common Causes of Memory Leaks

  • Static Collections: Holding references in static fields.
  • Listeners and Callbacks: Not removing event listeners.
  • Inner Classes: Holding implicit references to outer classes.
  • ThreadLocal Variables: Not cleaning up thread-local storage.
  • Caching: Unbounded caches without eviction policies.

Tools for Detecting Memory Leaks

  • VisualVM: GUI tool for monitoring JVM, heap dumps, and profiling.

  • jmap: Command-line tool to generate heap dumps.

    jmap -dump:live,format=b,file=heap.hprof <pid>
    
  • jstat: Monitor JVM statistics.

    jstat -gc <pid>
    
  • MAT (Memory Analyzer Tool): Analyze heap dumps to find leak suspects.

  • Eclipse Memory Analyzer: Open-source tool for heap analysis.

Monitoring Java Thread Count

ps -eLf | grep java | wc -l

This counts the number of Java threads running.

Prevention Tips

  • Use weak references (WeakReference, SoftReference) for caches.
  • Implement proper cleanup in finalize() or use try-with-resources.
  • Profile applications regularly with tools like VisualVM.
  • Avoid static collections; prefer instance variables.
  • Use libraries like Guava for caches with size limits.

References