Fundamentals 10 min read

Understanding Java Garbage Collection: Mechanisms, Memory Regions, and Practical Examples

This tutorial explains how Java's automatic garbage collection works, covering the roles of System.gc(), memory regions such as Eden, Survivor and Old generations, reference types, object finalization, and includes code examples that illustrate GC eligibility and OutOfMemoryError scenarios.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding Java Garbage Collection: Mechanisms, Memory Regions, and Practical Examples

This tutorial introduces the fundamentals of Java garbage collection (GC), an automatic process that manages runtime memory and frees programmers from manual allocation and deallocation.

GC can be hinted at using System.gc() or Runtime.gc() , but the JVM decides when to actually run collection based on heap state, especially the availability of the Eden space.

The heap is divided into regions: Eden where new objects are allocated, Survivor spaces (S0 and S1) where surviving objects are moved during Minor GC, and the Old (tenured) generation where long‑lived objects reside and are collected by Major GC.

Objects become eligible for GC when they are no longer reachable, which depends on their reference type: strong references prevent collection, soft references may be collected as a last resort, while weak and phantom references are always eligible.

Before an object’s memory is reclaimed, the JVM may invoke its finalize() method, though the order and timing are unspecified and exceptions in finalize() are ignored.

Example code demonstrates GC eligibility: class Animal { public static void main(String[] args) { Animal lion = new Animal(); System.out.println("Main is completed."); } protected void finalize() { System.out.println("Rest in Peace!"); } } In this case, if the compiler determines that lion is never used, it may assign lion = null to allow earlier collection.

A more complex example shows a circular reference (the "island of objects") that becomes eligible for GC only after all external references are cleared: class GCScope { GCScope t; static int i = 1; public static void main(String[] args) { GCScope t1 = new GCScope(); GCScope t2 = new GCScope(); GCScope t3 = new GCScope(); t1.t = t2; t2.t = t3; t3.t = t1; t1 = null; t2 = null; t3 = null; // all three objects become eligible for GC } protected void finalize() { System.out.println("Garbage collected from object" + i); i++; } }

An OutOfMemoryError example illustrates that careless code can exhaust heap space: import java.util.LinkedList; import java.util.List; public class GC { public static void main(String[] args) { List l = new LinkedList<>(); while (true) { l.add(new String("Hello, World")); } } } Running this program eventually throws java.lang.OutOfMemoryError: Java heap space .

The tutorial is part of a series; the next part will cover additional GC algorithms.

JavaJVMFinalizeOutOfMemoryErrorgarbage-collectionmemory-management
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.