
Day 2 - What is Memory Leak in Java?
A *memory leak in Java* occurs when objects that are no longer needed by the application are still referenced and, therefore, not garbage collected. This leads to increased memory usage and can eventually cause the application to run out of memory, leading to an **OutOfMemoryError**.
Common Causes of Memory Leak :-
Unclosed Resources (File, Database Connections, Streams)
When resources like files, database connections, or input/output streams are opened but not properly closed, they may not be released, consuming memory.
Static Collections (Holding Long-lived References)
Static fields hold references to objects for the lifetime of the application. If large objects are stored in static fields, they can cause memory leaks.
Large Object Caches
Using caches to store large objects (e.g., Map, List) can lead to memory leaks if the cache is never cleared or if there is no mechanism to remove old objects.
コメント