Fundamentals 7 min read

Understanding Java Heap Memory, GC, and OOM Through the Spring Festival Analogy

This article uses the Chinese Spring Festival story to explain Java's heap structure, young and old generations, large‑object allocation, and how excessive memory consumption leads to heap‑space OOM and stop‑the‑world garbage collection, providing code examples and practical mitigation tips.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Understanding Java Heap Memory, GC, and OOM Through the Spring Festival Analogy

Territory of the Nian Beast

The 年哥 (Nian) manages five main JVM areas: heap, method area, virtual machine stack, native method stack, and program counter. In the analogy, threads are villagers, and the heap is a shared area where villagers store data.

The heap is divided into the young generation and the old generation. The young generation further splits into Eden and two Survivor spaces with an 8:1 size ratio. The old generation holds long‑lived objects.

Stomach of the Nian Beast

Large objects ("big‑eaters") are allocated directly in the old generation because they require contiguous memory, which the fragmented young generation cannot provide. Typical examples are very long strings or huge arrays.

Massive Nian Beast Invasion

When many large objects are created, they occupy the old generation, exhausting its space and triggering a heap‑space OutOfMemoryError.

Code Demonstration

We create three 10 MB byte arrays, each representing a "Nian beast".

public class SpringFestivalOOM {
    public static void main(String[] args) {
        // Nian beasts 1/2/3, each 10 MB
        byte[] nianShou1 = new byte[10 * 1024 * 1024];
        byte[] nianShou2 = new byte[10 * 1024 * 1024];
        byte[] nianShou3 = new byte[10 * 1024 * 1024];
    }
}

Compile and run with a maximum heap of 20 MB:

javac SpringFestivalOOM.java
java -Xms20M -Xmx20M SpringFestivalOOM

The three 30 MB of allocations exceed the 20 MB limit, causing a heap‑space OutOfMemoryError.

Driving Away the Nian Beast

To avoid this situation, reduce the frequent creation of large objects. In real applications, massive short‑term data (e.g., a surge of complex order objects during a promotion) can quickly fill the old generation, trigger frequent GC, and cause "Stop‑the‑World" pauses that degrade performance.

Keeping Watch (守岁)

During the Spring Festival night, villagers stay at home and cannot do other work, mirroring the "Stop‑the‑World" pause where all application threads are halted while the GC runs.

Summary

Small objects (young generation) are allocated in the Eden/Survivor spaces; large objects (old generation) are allocated directly in the old generation.

Massive large‑object allocation on the Spring Festival night fills the old generation, causing heap‑space shortage and triggering GC.

"守岁" (staying home) represents the stop‑the‑world pause during GC where other threads cannot run.

Avoid frequent creation of large objects to prevent OOM and performance degradation in production.

The Spring Festival story also symbolizes the idea of "clearing the old and welcoming the new," akin to garbage collection.

JavaJVMGarbage Collectionoomheap memoryOld GenerationYoung Generation
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

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.