Java
Garbage Collector
Automatic Memory management system
Core Concept
The Garbage Collector (GC) is the JVM's automatic memory management system. It automatically deallocates (frees up) memory occupied by objects that are no longer actively used by the program.
GC primarily operates on the Heap Space memory area of the JVM, where all Java objects reside.
When is an object collected?
An object is considered "garbage" when it is unreachable or unreferenced—meaning there are no active references (variables) in the running program that point to that object's memory location. Check Beware of Variable Scope for more info.
Java is unique in that the programmer does not explicitly destroy objects or free memory (unlike C++). This avoids common memory leaks and dangling pointer errors.
Resources
- Oracle Java Documentation: Garbage Collection
- Baeldung: Guide to Java Garbage Collection
- GeeksforGeeks: Garbage Collector in Java