← Back to libraryQuestion 36 of 273
JavaAdvanced

Memory Leaks in Java

📖 Detailed Explanation:

A memory leak in Java occurs when objects are no longer being used by the application, but the Garbage Collector cannot remove them because they are still referenced by other objects (often static ones). Memory keeps accumulating, eventually causing OutOfMemoryError.

📎 Common Causes:
  • Static Collections holding references indefinitely (e.g., static List never cleared)
  • Unclosed Resources (Database connections, File streams, HTTP connections not closed)
  • Inner classes holding implicit references to outer class
  • Listeners and Observers not unregistered when no longer needed
  • ThreadLocal variables not properly cleaned up
🎯 Scenario-Based Interview Question:

How would you identify and debug a memory leak in a production Java application?