← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 3 of 19

Java Garbage Collection

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?

java
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");
	}
}

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

java
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");
	}
}

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

java
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");
	}
}

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)

java
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");
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.