In languages like C and C++, developers must manually allocate and deallocate memory using malloc() and free(). One forgotten free creates memory leaks; a premature free causes fatal segmentation faults. In Java, Garbage Collection (GC) is entirely automated, continuously tracking object lifecycles and reclaiming memory silently in the background.
An object is eligible for garbage collection when there is no path of references linking it to a GC Root. Primary GC Roots include:
| Phase | Generation | Event Name | Description |
|---|---|---|---|
| 1. Allocation | Eden Space | New Object | Objects are instantiated in Eden via new. |
| 2. Minor GC | Young (Eden + S0) | Scavenge | When Eden fills, Minor GC triggers. Live objects copy to Survivor space (S1) with age = 1. Eden is wiped clean. |
| 3. Promotion | Young $\rightarrow$ Old | Tenuring | Objects that survive repeated Minor GCs (tenuring threshold, default 15) are promoted to the Old (Tenured) Generation. |
| 4. Major / Full GC | Young + Old + Metaspace | Stop-The-World | Cleans Old Generation. Scans entire heap; triggers a Stop-The-World (STW) pause. |
| Collector | JVM Flag | Characteristics | Best Use Case |
|---|---|---|---|
| Serial GC | -XX:+UseSerialGC | Single-threaded, Stop-The-World | Small CLI tools, micro-containers (< 512 MB RAM) |
| Parallel GC | -XX:+UseParallelGC | Multi-threaded throughput-focused | Batch processing, offline scientific calculations |
| G1 GC (Default) | -XX:+UseG1GC | Region-based, predictable pauses | General enterprise web servers, heaps from 4 GB to 64 GB |
| ZGC (Modern) | -XX:+UseZGC | Concurrent, sub-millisecond pauses | Ultra-low latency financial, real-time gaming, > 16 GB heaps |
public class GCDemo {
public static void main(String[] args) {
// Case 1: Nullifying reference
String s1 = new String("Veewom");
s1 = null; // Original String object is now unreachable and eligible for GC
// Case 2: Reassigning reference
String a = new String("First");
String b = new String("Second");
a = b; // "First" object is now orphaned and eligible for GC
// Case 3: Objects created inside local method scope
createTempData(); // Any objects inside createTempData() without escaping are collected
// Suggesting GC (Never guaranteed in production!)
System.gc();
}
private static void createTempData() {
byte[] temp = new byte[1024]; // dies when method stack frame pops
}
}
System.gc() guarantee that garbage collection will run immediately?
System.gc() is merely a polite hint to the JVM that you would prefer garbage collection to run. The JVM specification explicitly gives no guarantee that the GC will execute immediately or at all. In enterprise production systems, System.gc() is often disabled using -XX:+DisableExplicitGC.finalize() method was officially deprecated in Java 9 and marked for removal due to unpredictable execution timing and resurrection vulnerabilities. Use AutoCloseable and Cleaner instead.Accelerate your Java mastery by studying these tightly connected core tutorials:
Eden, Survivor, and Tenured generation layout.
The Execution Engine and ClassLoader.
Understanding thread stacks as GC roots.
Modern concurrency impact on garbage collection.