Skip to main content

Java Garbage Collection Fundamentals

· One min read
Apache Wangye
Software developer and technical writer

Java garbage collection reclaims heap objects that are no longer reachable from a set of GC roots. Roots include active thread stacks, static fields, JNI references, and JVM-internal structures.

Reachability analysis is more accurate than reference counting because it can collect cycles. Weak, soft, and phantom references modify how the collector treats otherwise unreachable objects.

Generational collectors exploit the observation that most objects die young. New objects normally enter the young generation; survivors may be copied between survivor spaces and eventually promoted. Large or long-lived objects may be handled differently depending on the collector.

Collectors differ in throughput, pause behavior, concurrency, heap layout, and operational complexity. Serial and Parallel collectors favor simplicity or throughput; G1 divides the heap into regions; low-pause collectors such as ZGC and Shenandoah perform more work concurrently.

Do not tune from folklore. Enable unified GC logging, correlate pauses with allocation rate, promotion, humongous objects, CPU, container limits, and application latency, then change one setting at a time. A memory leak is usually unwanted object retention, not a failure of the collector.

Page views: --

Total views -- · Visitors --