Chapter 36 of 77

JVM Memory & Garbage Collection

Every time we write new Student(), Java finds some memory for the new object. We have created hundreds of objects by now, but we have never once written code to free that memory. In languages like C, the programmer must release memory by hand, and forgetting to do so is a very common source of bugs. So who cleans up in Java?

The answer is the Garbage Collector (GC), a part of the JVM that automatically finds objects that are no longer needed and reclaims their memory.

In the Data Types chapter, we saw a simple picture of stack and heap memory. Now that we understand classes, objects, constructors, static and recursion, we can look at the full picture: how the JVM organises memory, what happens during a method call, when an object becomes garbage, and how the garbage collector works. These are some of the most frequently asked Java interview topics.

JVM Memory Areas

When a Java program starts, the JVM sets aside memory and divides it into several run-time data areas. The Java Virtual Machine Specification defines them as follows:

┌──────────────────────── JVM memory ────────────────────────┐
│                                                            │
│  Shared by all threads           One per thread            │
│  ┌──────────────────────┐       ┌───────────────────────┐  │
│  │ Heap                 │       │ JVM Stack (frames)    │  │
│  │  objects and arrays  │       ├───────────────────────┤  │
│  ├──────────────────────┤       │ PC Register           │  │
│  │ Method Area          │       ├───────────────────────┤  │
│  │  class information,  │       │ Native Method Stack   │  │
│  │  method code,        │       └───────────────────────┘  │
│  │  constant pool       │                                  │
│  └──────────────────────┘                                  │
└────────────────────────────────────────────────────────────┘

Area

Shared or per thread

What it stores

Heap

Shared

All objects and arrays, including their instance variables. This is the area the garbage collector cleans.

Method Area

Shared

Information about each loaded class: its methods' bytecode, field and method details, and its run-time constant pool (constants and symbolic references used by the class).

JVM Stack

One per thread

One frame for each method call that has not finished yet. A frame holds that call's parameters and local variables.

PC Register

One per thread

The address of the bytecode instruction the thread is currently executing.

Native Method Stack

One per thread

Used when Java calls native code (code written in languages like C, inside the JVM or libraries).

A thread is one independent path of execution in a program. So far, our programs have had just one, the main thread. We will learn about running several threads at once in the Multithreading chapter. For now, remember that the heap is shared, while every thread has its own stack.

For interviews and for everyday programming, the two areas that matter most are the stack and the heap. Let's look at each in detail.

The Stack: Method Calls and Local Variables

Every time a method is called, the JVM pushes a new stack frame on top of the current thread's stack. The frame contains:

  • The method's parameters and local variables. For primitive types, the actual value is stored. For objects and arrays, only the reference is stored.

  • An operand stack, a small working area the JVM uses while calculating expressions.

  • Information needed to return to the method that called it.

When the method finishes, its frame is popped and all its local variables disappear instantly. No garbage collector is involved. This is why the stack is a LIFO (Last In, First Out) structure: the last method called is the first to finish.

main() calls a(), a() calls b():

            ┌─────────┐
            │  b()    │  ← top: currently running
            ├─────────┤
            │  a()    │  ← waiting for b() to return
            ├─────────┤
            │  main() │  ← waiting for a() to return
            └─────────┘

b() returns → its frame is popped → a() continues

This is exactly what happened in the Recursion chapter. Each recursive call pushes another frame. With no base case, frames keep piling up until the stack runs out of space, and the JVM throws a StackOverflowError:

class Main {
    static int depth = 0;

    static void goDeeper() {
        depth++;
        goDeeper();
    }

    public static void main(String[] args) {
        goDeeper();
    }
}

Output (the same line repeats many times):

Exception in thread "main" java.lang.StackOverflowError
	at Main.goDeeper(Main.java:6)
	at Main.goDeeper(Main.java:6)
	...

The stack size of each thread is fixed when the thread starts and can be changed with the JVM option -Xss (for example, java -Xss4m Main).

The Heap: Where Objects Live

Every object created with new, and every array, is stored on the heap. The heap is created when the JVM starts and is shared by all threads. Objects on the heap do not disappear when a method ends. They stay as long as something still refers to them, and the garbage collector removes them later.

A point that many students get wrong:

Instance variables live inside the object, on the heap, even when they are primitives.

As the Data Types chapter pointed out, the picture of a primitive sitting on the stack is about local variables only. For example, int marks inside a Student object is part of the object, so it is on the heap together with the object.

Two more details:

  • String pool: string literals such as "Aman" are kept in a special String pool, so identical literals share one object (as we saw in the Strings chapter). Since Java 7, the String pool is part of the heap.

  • static variables: a static variable belongs to the class, not to any object, so there is exactly one copy (as we learned in the Static Keyword chapter). Logically it is part of the class's data. In the standard HotSpot JVM, static variables have actually been stored on the heap, together with the class's Class object, since Java 7.

Tracing a Program Through Memory

Let's see where everything goes in a small program:

class Student {
    String name;
    int marks;

    Student(String name, int marks) {
        this.name = name;
        this.marks = marks;
    }
}

class Main {

    static void printTopper(Student a, Student b) {
        Student top = (a.marks >= b.marks) ? a : b;
        System.out.println("Topper: " + top.name + " (" + top.marks + ")");
    }

    public static void main(String[] args) {
        int count = 2;
        Student s1 = new Student("Aman", 85);
        Student s2 = new Student("Priya", 92);

        printTopper(s1, s2);

        s1 = s2;
        System.out.println("s1 now refers to: " + s1.name);
        System.out.println("Students created: " + count);
    }
}

Output:

Topper: Priya (92)
s1 now refers to: Priya
Students created: 2

While printTopper() is running, memory looks like this:

          STACK (main thread)                        HEAP

  ┌─────────────────────────────┐
  │ printTopper() frame         │
  │   a   ──────────────────────┼──────┐     ┌──────────────────────┐
  │   b   ──────────────────────┼───┐  └───► │ Student object #1    │
  │   top ──────────────────────┼─┐ │        │   name ──► "Aman"    │
  ├─────────────────────────────┤ │ │        │   marks = 85         │
  │ main() frame                │ │ │        └──────────────────────┘
  │   args  ──► String[] (empty)│ │ │              ▲
  │   count = 2                 │ │ │              │
  │   s1  ──────────────────────┼─┼─┼──────────────┘
  │   s2  ──────────────────────┼─┼─┴──────► ┌──────────────────────┐
  └─────────────────────────────┘ └────────► │ Student object #2    │
                                             │   name ──► "Priya"   │
                                             │   marks = 92         │
                                             └──────────────────────┘

Step by step:

  1. count is a local primitive, so the value 2 sits directly in main()'s frame.

  2. new Student(...) creates each object on the heap. s1 and s2 only hold references. The fields name and marks are inside the objects, on the heap.

  3. Calling printTopper(s1, s2) pushes a new frame. As we learned in the Method Parameters chapter, Java is pass-by-value, so a and b receive copies of the references. No objects are copied.

  4. When printTopper() returns, its frame (with a, b and top) is popped. The objects are untouched, because main() still refers to them.

  5. After s1 = s2;, both variables point to object #2. Nothing refers to object #1 (Aman) any more. It has become garbage, and the garbage collector is free to remove it.

When Does an Object Become Garbage?

The garbage collector does not care how old an object is or how big it is. It asks only one question: can the program still reach this object?

The search starts from GC roots, which are references the JVM knows are definitely in use:

  • Local variables and parameters in the frames of every running thread's stack

  • static variables of loaded classes

  • A few internal JVM references (such as active threads)

Any object that can be reached by following references from a GC root is alive. Every other object is eligible for garbage collection.

An object usually becomes unreachable in one of these four ways:

// 1. Setting the reference to null
Student s = new Student("Aman", 85);
s = null;                         // the Aman object is now unreachable

// 2. Reassigning the reference
Student t = new Student("Ravi", 70);
t = new Student("Neha", 88);      // the Ravi object is now unreachable

// 3. The method that created it returns
static void createTemp() {
    Student temp = new Student("Temp", 0);
}                                 // after return, the Temp object is unreachable

// 4. Island of isolation
class Node {
    Node next;
}

Node x = new Node();
Node y = new Node();
x.next = y;
y.next = x;                       // the two nodes refer to each other
x = null;
y = null;                         // both are unreachable, even though they still point to each other

Case 4 is a favourite interview question. The two Node objects still hold references to each other, but no GC root can reach either of them, so both are garbage. Java's collectors trace reachability from the roots, so cycles like this are not a problem. (A simple "reference counting" approach would fail here, because each object's count would stay at 1 forever.)

Important: "eligible" does not mean "removed right now". The JVM decides when to run the garbage collector, usually when some part of the heap starts filling up.

How Garbage Collection Works: Mark, Sweep, Compact

Almost every garbage collector is built from three basic steps:

  1. Mark: start from the GC roots, follow every reference, and mark each object reached as alive.

  2. Sweep: treat the memory of every unmarked object as free.

  3. Compact: slide (or copy) the live objects together, so that the free memory forms one large block. This makes allocating new objects fast and avoids "holes" (fragmentation).

Before:   [ A ][ b ][ C ][ d ][ e ][ F ]     (capital = reachable, small = garbage)
Mark:     [ A✓][ b ][ C✓][ d ][ e ][ F✓]
Sweep:    [ A ][   ][ C ][   ][   ][ F ]     (holes of free memory)
Compact:  [ A ][ C ][ F ][               ]   (one big free block)

Some of this work requires the application to be paused briefly. Such a pause is called a stop-the-world pause. A large part of GC research is about making these pauses shorter, by doing more of the work concurrently, while the program keeps running.

Generational Garbage Collection

Studies of real programs show that most objects die young. A temporary string built inside a loop, or an object used for one calculation, becomes garbage almost immediately. Only a few objects (like a cache or the application's settings) live for a long time. This observation is called the weak generational hypothesis.

So the heap is divided into generations, and the young part is collected often while the old part is collected rarely:

┌──────────────────── HEAP ────────────────────┐
│        Young Generation        │    Old      │
│ ┌────────────┬──────┬──────┐   │ Generation  │
│ │   Eden     │  S0  │  S1  │   │  (Tenured)  │
│ │ (new objs) │      │      │   │             │
│ └────────────┴──────┴──────┘   │             │
└──────────────────────────────────────────────┘
  1. New objects are created in Eden.

  2. When Eden fills up, a minor GC runs. The few live objects are copied into a survivor space (S0 or S1), and Eden is emptied in one go. Each time an object survives a minor GC, its age goes up by one.

  3. The two survivor spaces take turns: at each minor GC, the live objects from Eden and the current survivor space are copied into the other one.

  4. Once an object is old enough (in HotSpot, the age limit is at most 15 by default), it is promoted to the Old Generation.

  5. When the Old Generation fills up, a larger collection runs, often called a major GC. A full GC cleans the whole heap and is usually the slowest kind.

Because most young objects are already dead, a minor GC only has to copy a handful of survivors, which makes it very fast.

Class information is not on the heap in HotSpot. Before Java 8, it was stored in a fixed-size area called PermGen (Permanent Generation), which often caused OutOfMemoryError: PermGen space. Java 8 replaced PermGen with Metaspace, which lives in native memory (outside the heap) and grows as needed by default. Its size can be limited with -XX:MaxMetaspaceSize.

Garbage Collectors in the JVM

The HotSpot JVM (the one in Oracle JDK and OpenJDK) offers several collectors. You choose one with a JVM option, or let the JVM pick the default.

Collector

Option

Main idea

Good for

Serial GC

-XX:+UseSerialGC

One thread, stops the application while collecting

Small programs, small heaps

Parallel GC

-XX:+UseParallelGC

Many threads, still stop-the-world; focuses on total throughput

Batch jobs where short pauses don't matter

G1 GC

-XX:+UseG1GC

Splits the heap into many equal regions and collects the regions with the most garbage first; aims for a pause-time target

General purpose; the default

ZGC

-XX:+UseZGC

Does almost all work concurrently; pauses are typically well under a millisecond, even for very large heaps

Low-latency services, huge heaps

Shenandoah

-XX:+UseShenandoahGC

Also mostly concurrent with very short pauses

Low-latency services (available in most OpenJDK builds)

A few version facts that interviewers like:

  • G1 has been the default collector since Java 9. Until Java 26, the JVM still picked Serial GC on very small machines (one CPU or less than about 1.8 GB of memory). Since Java 27, G1 is the default in all environments.

  • ZGC and Shenandoah became production-ready in Java 15. Generational ZGC arrived in Java 21 and became ZGC's only mode in Java 24.

  • The old CMS (Concurrent Mark Sweep) collector was deprecated in Java 9 and removed in Java 14.

Seeing Memory from a Java Program

The Runtime class lets a program ask the JVM about its heap:

class Main {
    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();
        long mb = 1024 * 1024;

        System.out.println("Max heap   : " + runtime.maxMemory() / mb + " MB");
        System.out.println("Total heap : " + runtime.totalMemory() / mb + " MB");

        long before = (runtime.totalMemory() - runtime.freeMemory()) / mb;
        System.out.println("Used before: " + before + " MB");

        for (int i = 0; i < 100_000; i++) {
            int[] temp = new int[1000];   // about 4 KB each, garbage right away
        }

        long after = (runtime.totalMemory() - runtime.freeMemory()) / mb;
        System.out.println("Used after creating garbage: " + after + " MB");

        System.gc();   // only a request

        long afterGc = (runtime.totalMemory() - runtime.freeMemory()) / mb;
        System.out.println("Used after System.gc(): " + afterGc + " MB");
    }
}

One possible output (the numbers depend on your machine, Java version and collector):

Max heap   : 6100 MB
Total heap : 384 MB
Used before: 1 MB
Used after creating garbage: 86 MB
Used after System.gc(): 0 MB

What this shows:

  • maxMemory() is the largest the heap may grow. If you don't set it, the JVM usually picks about one quarter of the machine's physical memory.

  • totalMemory() is how much heap the JVM has reserved right now, and freeMemory() is the unused part of it.

  • The loop created 100,000 arrays, each unreachable as soon as the next loop iteration starts. Used memory went up, and after a collection it came back down.

System.gc(): Only a Request

System.gc() (or Runtime.getRuntime().gc()) suggests that the JVM run the garbage collector. The JVM is allowed to ignore it, and it can even be disabled with the option -XX:+DisableExplicitGC. There is no way to force garbage collection in Java.

In real applications, you should almost never call System.gc(). The JVM knows much better when to collect, and a forced full GC can cause a long pause.

Also remember from the final Keyword chapter that finalize() is deprecated. Never rely on the garbage collector to close files or network connections, because you don't know when (or whether) an object will be collected. Close them yourself, for example with try-with-resources, which we will see in the File Handling chapter.

When Memory Runs Out

If live objects fill the whole heap and the GC cannot free enough space, the JVM throws an OutOfMemoryError. We can see this by giving the JVM a tiny heap with -Xmx:

class Main {
    public static void main(String[] args) {
        System.out.println("Creating a big array...");
        long[] big = new long[10_000_000];   // 10 million longs = about 80 MB
        System.out.println("Created array of length " + big.length);
    }
}

Running it with a 256 MB heap works:

java -Xmx256m Main
Creating a big array...
Created array of length 10000000

Running it with a 32 MB heap does not:

java -Xmx32m Main
Creating a big array...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at Main.main(Main.java:4)

Each long takes 8 bytes, so 10,000,000 × 8 = 80,000,000 bytes, about 80 MB. That can never fit in a 32 MB heap.

Notice the name: it is an Error, not an exception you are expected to handle. StackOverflowError and OutOfMemoryError usually mean there is a bug (infinite recursion, a leak) or the JVM really needs more memory.

Can Java Have Memory Leaks?

Yes. The garbage collector removes only unreachable objects. If your program keeps references to objects it no longer needs, the GC must keep them too. Common causes:

  • Adding objects to a static list or map and never removing them. Static variables are GC roots, so everything they refer to stays alive for the whole program.

  • Caches that grow forever.

  • Files, database connections or other resources that are opened but never closed.

The memory slowly fills up until the program fails with OutOfMemoryError. Tools such as heap dumps (-XX:+HeapDumpOnOutOfMemoryError), VisualVM and Java Flight Recorder help find such leaks.

Useful JVM Memory Options

Option

Meaning

Example

-Xms

Initial heap size

-Xms256m

-Xmx

Maximum heap size

-Xmx2g

-Xss

Stack size of each thread

-Xss1m

-XX:MaxMetaspaceSize

Upper limit for Metaspace

-XX:MaxMetaspaceSize=256m

-XX:+UseG1GC, -XX:+UseZGC

Choose a garbage collector

java -XX:+UseZGC Main

-Xlog:gc

Print a line for every GC (Java 9+)

java -Xlog:gc Main

JVM options always go before the class name, as we saw in the Command-Line Arguments chapter: java -Xmx512m Main arg1 arg2.

Stack vs Heap

Feature

Stack

Heap

Stores

Method frames: parameters, local variables (primitive values and references)

All objects and arrays, including their instance variables

Shared?

No, one stack per thread

Yes, shared by all threads

Freed when

The method returns (automatic, instant)

The garbage collector finds the object unreachable

Order

LIFO

No particular order

Size

Small (set with -Xss)

Large (set with -Xms / -Xmx)

When full

StackOverflowError

OutOfMemoryError: Java heap space

Speed

Very fast (just move the top of the stack)

Fast to allocate, but needs GC work to clean up

(One advanced note: the JIT compiler can sometimes avoid creating an object on the heap at all, if it can prove the object never leaves a method. This optimisation is called escape analysis. It never changes how your program behaves, so you should always reason with the model above.)

Common Mistakes

  • "All primitives are on the stack." Only local primitives are. Primitive instance variables are inside their object on the heap.

  • "Setting a variable to null deletes the object." It only removes one reference. The object is removed later, and only if no other reference reaches it.

  • "System.gc() forces garbage collection." It is only a request.

  • "Java has no memory leaks because of the GC." Objects that are still referenced (for example, from a static list) are never collected.

  • Relying on finalize() or the GC to release files and connections. Close resources yourself.

Interview Corner

  • What is garbage collection? The automatic process by which the JVM finds objects that are no longer reachable from any GC root and reclaims their memory.

  • What are GC roots? References the JVM knows are live: local variables and parameters in active stack frames, static variables, active threads and a few JVM-internal references.

  • When is an object eligible for GC? When it is no longer reachable from any GC root: its reference was set to null or reassigned, the method holding it returned, or it is only part of an island of isolation.

  • Can we force garbage collection? No. System.gc() is only a hint.

  • What is the difference between minor and major GC? A minor GC cleans the Young Generation (fast and frequent). A major GC cleans the Old Generation, and a full GC cleans the whole heap (slower and rarer).

  • PermGen vs Metaspace? PermGen was a fixed-size area for class metadata, removed in Java 8. Metaspace replaced it, lives in native memory, and grows automatically by default.

  • Where is the String pool? On the heap, since Java 7.

  • Which GC is the default? G1, since Java 9.

  • StackOverflowError vs OutOfMemoryError? The first means a thread's stack is full (usually deep or infinite recursion). The second means the heap (or Metaspace) has no room for new data even after garbage collection.

Quick Revision

Stack = one per thread, holds method frames, freed when a method returns. Heap = shared, holds all objects, cleaned by the garbage collector. An object is garbage when no GC root can reach it.

  • JVM areas: Heap, Method Area (Metaspace in HotSpot), and per thread: JVM Stack, PC Register, Native Method Stack.

  • Instance variables live on the heap with their object; local variables live in stack frames.

  • GC = mark, sweep, compact; young objects are collected often (minor GC), old objects rarely (major/full GC).

  • G1 is the default collector; ZGC and Shenandoah focus on very short pauses.

  • System.gc() is just a request; Java can still leak memory through forgotten references.