Java Memory Management and Garbage Collection (Simple Version)

Anni HuangAnni Huang
3 min read

Here's a summarized explanation of Java memory management and garbage collection:


✅ Type of Memory: Stack vs Heap

Memory TypeDescription
StackStores method call frames, local variables, and references. Follows LIFO. Each thread has its own stack.
HeapStores all objects and class-level variables. Shared among all threads. Managed by the Garbage Collector (GC).

📦 What Kind of Data Is Stored?

🧠 Stack

  • Primitive values (int, boolean, etc.)
  • References to heap objects Example:
int x = 5;          // x stored in stack
String s = "abc";   // reference stored in stack, actual String in heap

🧠 Heap

  • All objects and arrays Example:
Person p = new Person();  // Person object stored in heap

🔗 Types of References

Reference TypeDescription
StrongDefault. Prevents GC from collecting the object.
WeakAllows GC to collect object if only weak references exist.
SoftGC collects when memory is low. Used for caches.

💪 Strong Reference

String s = new String("hello");

As long as s is in scope, the object won’t be garbage collected.

🪶 Weak Reference

WeakReference<String> weakStr = new WeakReference<>(new String("weak"));

Collected when GC runs, even if memory is sufficient.

🌬️ Soft Reference

SoftReference<String> softStr = new SoftReference<>(new String("soft"));

Only collected if JVM runs low on memory.


🧱 Heap Memory Structure (Managed by GC)

1. Young Generation (Young Gen)

  • Where new objects are allocated.
  • Collected frequently (minor GC).
  • Subdivided into:

    • Eden: Where objects are first created.
    • Survivor Spaces (S0, S1): Objects that survive Eden collection.

2. Old Generation (Tenured Gen)

  • Stores long-living objects.
  • Collected less frequently (major GC).

3. Metaspace (Java 8+)

  • Stores class metadata (formerly PermGen).
  • Resides in native memory (outside heap).

🧹 How Garbage Collector Works (Simplified)

Example:

Person p1 = new Person();  // Allocated in Eden
p1 = null;                 // No strong reference
System.gc();               // Hint to GC to run

GC Workflow:

  1. Minor GC: Cleans Eden → survivors go to S0/S1.
  2. If object survives multiple GCs → moved to Old Gen.
  3. Major GC: Cleans Old Gen.
  4. GC frees objects with no strong references.

🚮 Types of Garbage Collectors

GC TypeDescription
Serial GC (Single GC)Single-threaded. Good for small apps. Flag: -XX:+UseSerialGC
Parallel GCMulti-threaded, optimized for throughput. Flag: -XX:+UseParallelGC
CMS (Concurrent Mark and Sweep)Low pause GC. Concurrent with app threads. Flag: -XX:+UseConcMarkSweepGC (deprecated in Java 9+)
G1 GC (Garbage First)Breaks heap into regions, balances pause time and throughput. Flag: -XX:+UseG1GC

Would you like a visual diagram of this, or a Markdown/PDF cheat sheet?

0
Subscribe to my newsletter

Read articles from Anni Huang directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anni Huang
Anni Huang

I am Anni HUANG, a software engineer with 3 years of experience in IDE development and Chatbot.