SyntaxPilot
Courses/Java/Lesson 43
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Java · Lesson 43 of 51

JVM internals: class loading, memory areas, garbage collection

queued

These are the notes for Java episode 43. Since episode 1 we've typed java Main and let the JVM run our code. Today we open it up, using a big restaurant: recipe cards are fetched only when the first order for a dish arrives (lazy class loading), three shelves of cards and a rule to ask the senior first (class loaders and parent delegation), a notepad per waiter and one shared dining hall (stack and heap), and a busboy who clears abandoned tables (the garbage collector). We fill the stack (StackOverflowError), make 5,000 MB of paper cups in a much smaller heap without a crash, and then crash it with a static shelf that never lets go (OutOfMemoryError: Java heap space, a memory leak). Every example was run with javac 21 on the default G1 collector.

Some numbers here depend on the machine: recursion depth, heap size and how often the GC runs. The outputs below are real runs; yours will show different numbers with the same meaning.

When is a class loaded?

Real-life example: five hundred recipe cards. A restaurant with 500 dishes doesn't pull out all 500 recipe cards when the doors open; it fetches a card when the first order for that dish arrives. A Java program is the same: your classes plus thousands from the Java library.

A static block (episode 16) runs once, when its class is initialized, so it can announce the moment of loading.

class Menu {
    static { System.out.println("  [Menu class loaded]"); }
    static String today() { return "masala dosa"; }
}
class Bill {
    static { System.out.println("  [Bill class loaded]"); }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Stall opens");
        System.out.println("Today: " + Menu.today());
        System.out.println("Again: " + Menu.today());
        System.out.println("Stall closes");      // Bill is never used
    }
}

Output:

Stall opens
  [Menu class loaded]
Today: masala dosa
Again: masala dosa
Stall closes
  • Menu is loaded only at the first call to Menu.today(), and only once.
  • Bill is never used, so it is never loaded.

This is lazy loading: the JVM loads a class the first time it is really needed (creating an object, calling a static method, using a non-constant static field, a subclass being loaded, or reflection like Class.forName), and keeps it for the rest of the run.

ClassNotFoundException

Class.forName("name") loads a class by its name at run time. Frameworks such as Spring and JDBC drivers do this, reading class names from configuration.

public class Main {
    public static void main(String[] args) throws Exception {
        Class<?> c = Class.forName("Biryani");    // load a class by its name
        System.out.println("Loaded " + c.getName());
    }
}

Output:

Exception in thread "main" java.lang.ClassNotFoundException: Biryani
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:423)
    at java.base/java.lang.Class.forName(Class.java:414)
    at Main.main(Main.java:3)

No class loader could find a class called Biryani. The stack trace shows who looked: BuiltinClassLoader, AppClassLoader, ClassLoader.loadClass.

Error When Typical cause
ClassNotFoundException (checked exception) loading a class by name (Class.forName, loadClass) fails wrong name or package, missing jar on the classpath
NoClassDefFoundError (error) a class that existed at compile time is missing at run time, or its static setup failed earlier a jar left out when running, an exception in a static block

Class loaders

Real-life example: three shelves of recipe cards.

In the kitchen In the JVM Loads
the head chef's own book bootstrap class loader the core classes: java.lang.String, Object, java.util (module java.base)
the restaurant's shelf platform class loader other JDK modules, e.g. java.sql
today's special cards application (system) class loader your classes, from the classpath (the folders and jar files where your compiled code lives)
ask the senior first parent delegation a loader first asks its parent; it searches itself only if the parent can't find the class

Delegation is why nobody can sneak in a fake java.lang.String: the bootstrap loader always answers first.

public class Main {
    public static void main(String[] args) {
        ClassLoader app = Main.class.getClassLoader();
        System.out.println("Main loaded by:   " + app.getName());
        System.out.println("its parent:       " + app.getParent().getName());
        System.out.println("String loaded by: " + String.class.getClassLoader());
    }
}

Output:

Main loaded by:   app
its parent:       platform
String loaded by: null

null means the bootstrap loader: it is built into the JVM in native code, so there is no Java object for it.

What "loading a class" really means

Stage Step What happens
Loading find the .class file and read its bytecode; create the class's data in metaspace
Linking Verify check the bytecode is valid and safe (a broken or tampered file can't crash the JVM)
Prepare make room for static fields and give them default values (0, false, null)
Resolve turn symbolic names like Menu into real references to loaded classes
Initialization set static fields to their real values and run static blocks, top to bottom, once

JVM memory areas

Real-life example: the restaurant floor plan.

In the restaurant In the JVM Holds
each waiter's notepad, a page per table stack, one per thread one frame per method call: its local variables, parameters and partial results
the shared dining hall heap, shared every object and array created with new
the recipe shelf metaspace, shared class data: method code, field and method info, static variables' class data

The full map:

Area Shared or per thread Holds When it's full
Heap shared by all threads objects, arrays OutOfMemoryError: Java heap space
Metaspace shared class metadata (replaced PermGen in Java 8) OutOfMemoryError: Metaspace
JVM stack per thread frames: locals, parameters StackOverflowError
PC register per thread which bytecode instruction the thread is on
Native method stack per thread calls into native (C) code inside the JVM
void order() {
    int qty = 2;                    // qty lives in order()'s frame, on the stack
    Order o = new Order("chai");    // the Order object is on the heap;
}                                   // the reference o is on the stack

When order() returns, its frame is removed, qty and o disappear, and the Order object becomes unreachable (unless something else refers to it).

StackOverflowError

public class Main {
    static int depth = 0;
    static void serve() {
        depth++;
        serve();                 // no base case
    }
    public static void main(String[] args) {
        try {
            serve();
        } catch (StackOverflowError e) {
            System.out.println("Stack full after " + depth + " calls: " + e);
        }
    }
}

Output:

Stack full after 10732 calls: java.lang.StackOverflowError

serve() calls itself with no base case (episode 12). Each call adds a frame; every thread's stack is small and fixed, so it fills up. The depth depends on the machine, the stack size (-Xss) and how big each frame is. Fix: a base case, or a loop.

Garbage collection

5,000 MB of cups and no crash

public class Main {
    public static void main(String[] args) {
        long made = 0;
        for (int i = 0; i < 5_000; i++) {
            byte[] cup = new byte[1_000_000];    // a 1 MB paper cup
            made += cup.length;                   // used once, then thrown away
        }
        System.out.println("Cups made: " + made / 1_000_000 + " MB worth");
        long maxMb = Runtime.getRuntime().maxMemory() / 1_000_000;
        System.out.println("Heap limit: " + maxMb + " MB");
    }
}

Output:

Cups made: 5000 MB worth
Heap limit: 1566 MB

Runtime.getRuntime().maxMemory() is the largest the heap may grow. We created 5,000 MB of objects in a heap with a much smaller limit, and nothing crashed: someone cleared the old cups.

Real-life example: clearing the tables.

In the restaurant In the JVM
plates pile up on tables objects pile up on the heap
is a customer still holding this plate? is the object reachable?
clear tables nobody is using unreachable objects are garbage; their memory is reclaimed
push free tables together for a big group compacting: move live objects together to leave one big free space

How the collector decides

Step What happens
GC roots start from references that are certainly alive: local variables in every thread's stack frames, static fields, active threads
Mark follow every reference from the roots and mark each object reached
Sweep free the memory of every unmarked (unreachable) object
Compact move live objects together so big objects fit

Reachable from a root means alive; unreachable means garbage, even if it still holds data. In our loop, cup is replaced every pass, so each old cup becomes unreachable.

Generations. Most objects die young (like paper cups). So the heap is split:

Part Holds Cleaned by
Young generation (eden + survivor spaces) new objects frequent, fast minor GC
Old generation objects that survived several minor GCs less frequent major / mixed GC

You never free memory yourself in Java (unlike free() in C). G1 ("garbage first") is the default collector in modern Java: it splits the heap into many regions and cleans those with the most garbage first. Others: Parallel (throughput), ZGC and Shenandoah (very short pauses).

Catching the collector at work

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5_000; i++) {
            byte[] cup = new byte[1_000_000];
        }
        for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans())
            System.out.println(gc.getName() + ": ran " + gc.getCollectionCount() + " times");
    }
}

Output (one run; your counts will differ):

G1 Young Generation: ran 28 times
G1 Concurrent GC: ran 0 times
G1 Old Generation: ran 0 times

ManagementFactory.getGarbageCollectorMXBeans() returns monitoring objects (MXBeans) for each collector. The young-generation collector ran many times; the old generation never needed to, because no cup lived long.

OutOfMemoryError and memory leaks

import java.util.ArrayList;
import java.util.List;

public class Main {
    static List<byte[]> shelf = new ArrayList<>();   // never emptied
    public static void main(String[] args) {
        try {
            for (int i = 1; i <= 5_000; i++) {
                shelf.add(new byte[1_000_000]);       // keep every 1 MB cup
                if (i % 500 == 0) System.out.println("Kept " + i + " MB");
            }
        } catch (OutOfMemoryError e) {
            int kept = shelf.size();
            shelf = null;                             // let the cups go
            System.out.println("Crashed after keeping " + kept + " MB: " + e);
        }
    }
}

Output:

Kept 500 MB
Kept 1000 MB
Crashed after keeping 1492 MB: java.lang.OutOfMemoryError: Java heap space

Every cup is still reachable: from a static field (a GC root), through the list. So nothing is garbage and the heap fills up. We catch the error only to print how far we got; normally you don't catch OutOfMemoryError.

A memory leak in Java is objects you no longer need but still reference. Usual suspects:

  • static collections that only grow,
  • caches without a size limit,
  • listeners or callbacks added but never removed,
  • long-lived objects holding references to short-lived ones.

Fix: drop the reference (remove entries, limit the cache, set a field to null when finished). Heap size flags: java -Xms256m -Xmx1g Main (starting and maximum heap). A bigger heap only delays a leak.

Common mistakes (and what you see)

Mistake What you see Why Fix
Class.forName("Biryani") java.lang.ClassNotFoundException: Biryani no loader can find that class check the name, package and classpath
running without a jar you compiled against java.lang.NoClassDefFoundError: ... the class was there at compile time only add it to the classpath
void serve() { serve(); } java.lang.StackOverflowError endless recursion fills the stack a base case, or a loop
a static list that only grows java.lang.OutOfMemoryError: Java heap space everything is still reachable remove what you no longer need
System.gc(); to "free memory now" no error, often no visible effect it's only a request the JVM may ignore drop references; let the GC decide
expecting finalize() to clean up no error: it may run late or never (deprecated) GC timing is not guaranteed try-with-resources for files and connections

Mini project: a memory report

public class Main {
    static long usedMb() {
        Runtime rt = Runtime.getRuntime();
        return (rt.totalMemory() - rt.freeMemory()) / 1_000_000;
    }
    public static void main(String[] args) {
        System.out.println("Heap max:        " + Runtime.getRuntime().maxMemory() / 1_000_000 + " MB");
        System.out.println("Used at start:   " + usedMb() + " MB");
        byte[][] tray = new byte[300][];
        for (int i = 0; i < tray.length; i++) tray[i] = new byte[1_000_000];   // 300 MB, reachable
        System.out.println("Holding 300 cups: " + usedMb() + " MB");
        tray = null;                                                        // now unreachable
        System.gc();                                                        // a request, not an order
        System.out.println("After letting go: " + usedMb() + " MB");
    }
}

Output (one run):

Heap max:        1566 MB
Used at start:   1 MB
Holding 300 cups: 315 MB
After letting go: 1 MB

Used heap = totalMemory() - freeMemory(). Holding 300 one-megabyte cups uses about 300 MB more; after tray = null they are unreachable, and after a GC the used heap drops back. (System.gc() is a request; on G1 it usually runs a full collection, but don't rely on it in real code.)

Practice

  1. Show that the first use of a static field loads a class.
  2. Print the class loader of java.sql.Date.
  3. Measure recursion depth with more local variables.
  4. Write a leaky cache, then fix it with a size limit.
Solution 1
class Prices {
    static { System.out.println("  [Prices class loaded]"); }
    static int chai = 15;
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Before");
        System.out.println("Chai costs " + Prices.chai);   // first use of a static field
        System.out.println("After");
    }
}

Output:

Before
  [Prices class loaded]
Chai costs 15
After

(If chai were static final int chai = 15;, a compile-time constant, the compiler copies the value into Main and Prices would not be loaded at all.)

Solution 2
public class Main {
    public static void main(String[] args) {
        System.out.println("java.sql.Date: " + java.sql.Date.class.getClassLoader().getName());
        System.out.println("java.util.Date: " + java.util.Date.class.getClassLoader());
        System.out.println("Main: " + Main.class.getClassLoader().getName());
    }
}

Output:

java.sql.Date: platform
java.util.Date: null
Main: app

java.sql is a separate JDK module loaded by the platform loader; java.util.Date is in java.base, loaded by bootstrap (null).

Solution 3
public class Main {
    static int small = 0, big = 0;
    static void light() { small++; light(); }
    static void heavy(long a, long b, long c, long d) {
        long e = a + 1, f = b + 1, g = c + 1, h = d + 1;
        big++;
        heavy(e, f, g, h);
    }
    public static void main(String[] args) {
        try { light(); } catch (StackOverflowError err) { }
        try { heavy(1, 2, 3, 4); } catch (StackOverflowError err) { }
        System.out.println("light frames: " + small);
        System.out.println("heavy frames: " + big);
        System.out.println("bigger frames -> fewer fit: " + (big < small));
    }
}

Output (one run; numbers vary):

light frames: 23306
heavy frames: 9518
bigger frames -> fewer fit: true

More parameters and locals make each frame bigger, so fewer frames fit in the same stack.

Solution 4

Leaky (no limit):

import java.util.*;

public class Main {
    static Map<Integer, byte[]> cache = new HashMap<>();   // no limit
    public static void main(String[] args) {
        try {
            for (int order = 1; order <= 5_000; order++)
                cache.put(order, new byte[1_000_000]);
        } catch (OutOfMemoryError e) {
            int n = cache.size();
            cache = null;
            System.out.println("Crashed after " + n + " receipts: " + e);
        }
    }
}

Output:

Crashed after 1491 receipts: java.lang.OutOfMemoryError: Java heap space

Fixed with a size limit (LinkedHashMap.removeEldestEntry):

import java.util.*;

public class Main {
    static final int LIMIT = 100;
    // a cache that forgets its oldest entry once it holds more than LIMIT
    static Map<Integer, byte[]> cache = new LinkedHashMap<>() {
        @Override
        protected boolean removeEldestEntry(Map.Entry<Integer, byte[]> eldest) {
            return size() > LIMIT;
        }
    };
    public static void main(String[] args) {
        for (int order = 1; order <= 5_000; order++)
            cache.put(order, new byte[1_000_000]);     // 1 MB receipt image per order
        System.out.println("Orders processed: 5000");
        System.out.println("Receipts kept in cache: " + cache.size());
        System.out.println("Oldest kept: order " + cache.keySet().iterator().next());
    }
}

Output:

Orders processed: 5000
Receipts kept in cache: 100
Oldest kept: order 4901

The cache keeps only the newest 100 receipts; older ones are removed, become unreachable, and the GC frees them.

Quick quiz

Order a = new Order("chai");
Order b = a;
a = new Order("samosa");
// is the "chai" order garbage?

A. Yes, a no longer points to it B. No, b still points to it C. Yes, but only after System.gc() D. No, objects are never collected

Answer

B. b still holds a reference to the "chai" order, so it is reachable from a GC root (a local variable). It becomes garbage only when nothing reaches it, e.g. after b = null; too.

Interview questions

  1. Explain the JVM architecture. Class loader subsystem → runtime data areas (heap, metaspace, stacks, PC registers, native stacks) → execution engine (interpreter, JIT compiler, garbage collector) → native interface.
  2. What are the class loaders and what is parent delegation? Bootstrap, platform, application; each delegates to its parent first, which protects core classes and avoids loading a class twice.
  3. What are the phases of class loading? Loading, linking (verify, prepare, resolve), initialization.
  4. ClassNotFoundException vs NoClassDefFoundError? The first is a checked exception when loading by name fails; the second is an error when a class present at compile time is missing (or failed to initialize) at run time.
  5. Stack vs heap? Stack: per thread, frames with locals, LIFO, small, freed automatically on return. Heap: shared, objects, managed by the GC.
  6. What replaced PermGen? Metaspace (Java 8), in native memory, growing as needed (limit with -XX:MaxMetaspaceSize).
  7. How does the GC decide what to collect? Reachability from GC roots; unreachable objects are garbage (mark, sweep, compact).
  8. Why generational GC? Most objects die young, so collecting the young generation often and cheaply frees most garbage quickly.
  9. Can Java have memory leaks? Yes: objects that are still referenced but no longer needed, e.g. a growing static map.
  10. Does System.gc() force garbage collection? No, it's a request the JVM may ignore.
  11. What do -Xms, -Xmx and -Xss set? Initial heap, maximum heap, thread stack size.
  12. What is the JIT compiler? The part of the execution engine that compiles frequently run bytecode to machine code while the program runs.

Glossary

Term Meaning
JVM Java Virtual Machine: runs bytecode, manages memory
Bytecode the instructions in a .class file
Class loading finding a class's bytecode and making it usable
Lazy loading loading a class only when it's first needed
Static block static { ... }: code run once when the class is initialized
Class.forName load a class by its name at run time
ClassNotFoundException no class loader found a class asked for by name
NoClassDefFoundError a class needed at run time is missing or failed to initialize
Class loader an object that loads classes
Bootstrap class loader built-in loader for the core classes (java.base); shown as null
Platform class loader loads other JDK modules
Application class loader loads your classes from the classpath
Classpath the folders and jars where the JVM looks for your classes
Parent delegation a loader asks its parent before searching itself
Linking verify, prepare and resolve a loaded class
Verification checking bytecode is valid and safe
Initialization setting static fields and running static blocks
Stack per-thread memory of method frames
Frame one method call's locals and parameters on the stack
Heap shared memory where objects live
Metaspace memory for class metadata
PC register per-thread pointer to the current instruction
Native method stack per-thread stack for native (C) code
StackOverflowError the stack ran out of room, usually endless recursion
Garbage collector (GC) the part of the JVM that frees memory of unreachable objects
Garbage an object no live part of the program can reach
Reachable can be reached by following references from a GC root
GC root a starting reference that is certainly alive: stack locals, static fields, threads
Mark and sweep mark reachable objects, free the rest
Compaction moving live objects together to remove gaps
Young / old generation heap areas for new objects / long-lived objects
Minor GC a collection of the young generation
G1 "garbage first", the default collector
MXBean a monitoring object from java.lang.management
OutOfMemoryError the JVM couldn't get the memory it needed
Memory leak objects still referenced but no longer needed
-Xms / -Xmx / -Xss flags for initial heap, maximum heap, thread stack size
JIT compiler compiles hot bytecode to machine code at run time

Summary

  • Classes load lazily, on first use, once: loading → linking (verify, prepare, resolve) → initialization (static blocks).
  • Bootstrap, platform and application class loaders; each asks its parent first. ClassNotFoundException means none found it.
  • Each thread has a stack of frames; the heap (objects) and metaspace (classes) are shared. Endless recursion → StackOverflowError.
  • The garbage collector frees objects that are unreachable from GC roots (mark, sweep, compact), mostly in the young generation. G1 is the default.
  • Anything you still reference stays: a growing static collection is a memory leak and ends in OutOfMemoryError: Java heap space.
  • Next episode: unit testing with JUnit.