Java Garbage Collection
In this page:
What is Garbage Collection?
Garbage collection is the JVM's automatic process for monitoring the heap and reclaiming memory occupied by objects that are no longer reachable from any live reference, freeing developers from manually deallocating memory the way C or C++ requires.
Example: What is Garbage Collection?
public class Main {
public static void main(String[] args) {
Object obj = new Object();
obj = null; // no longer reachable -- eligible for automatic reclamation
System.gc(); // requests garbage collection (JVM decides when it actually runs)
System.out.println("Unreachable object may be collected");
}
}
Login to try C/C++/Java/PHP code in the editor
Memory Generations
The JVM divides the heap into generations -- typically a Young generation for newly created, short-lived objects and an Old (Tenured) generation for objects that have survived multiple collection cycles -- because most objects die young, and separating them lets the collector scan the Young generation quickly and often.
Example: Memory Generations
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
Object shortLived = new Object(); // most objects like this die young, in the Young generation
}
System.out.println("Short-lived objects collected quickly from the Young generation");
}
}
Login to try C/C++/Java/PHP code in the editor
G1 Garbage Collector
The G1 (Garbage First) collector divides the heap into many small regions and prioritizes collecting the regions with the most garbage first, which lets it target the highest-value cleanup work and keep individual pause times short and predictable even on large heaps.
Example: G1 Garbage Collector
public class Main {
public static void main(String[] args) {
// Run with: java -XX:+UseG1GC Main
System.out.println("G1 divides the heap into regions, collecting the most-garbage ones first");
}
}
Login to try C/C++/Java/PHP code in the editor
Z Garbage Collector (ZGC)
The Z Garbage Collector (ZGC) is designed for very large heaps and extremely low pause times by performing almost all of its work concurrently with the running application, trading some CPU overhead for pauses that stay in the single-digit milliseconds regardless of heap size.
Example: Z Garbage Collector (ZGC)
public class Main {
public static void main(String[] args) {
// Run with: java -XX:+UseZGC Main
System.out.println("ZGC targets single-digit millisecond pauses on very large heaps");
}
}
Login to try C/C++/Java/PHP code in the editor
Weak and Soft References
Weak references let the collector reclaim an object as soon as no strong references remain, even if a weak reference still points to it -- useful for caches you don't want to prevent garbage collection. Soft references are similar but more forgiving: they're only cleared when the JVM is actually running low on memory.
Example: Weak and Soft References
import java.lang.ref.*;
public class Main {
public static void main(String[] args) {
Object strong = new Object();
WeakReference<Object> weak = new WeakReference<>(strong); // reclaimed as soon as no strong refs remain
SoftReference<Object> soft = new SoftReference<>(strong); // cleared only under memory pressure
System.out.println(weak.get() != null);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing