Skip to content
Tutorials Center
My learning

Topics

Lesson 151 of 201 ยท JVM and Advanced Topics

Memory and Garbage Collection

Understand Java memory - stack vs heap, references and reachability, generational garbage collection, G1 and ZGC, memory leaks, Runtime stats and soft and weak references.

In C you call malloc and free; forget the free and you leak memory, call it twice and you crash. Java takes that job away from you: you create objects with new, and the JVM's garbage collector (GC) finds objects that your program can no longer reach and reclaims their memory automatically. That's one of the biggest reasons Java programs are safe from whole classes of bugs, such as dangling pointers, double frees and buffer overruns.

Automatic doesn't mean magic, though. Java programs can still run out of memory, still leak (by keeping references they no longer need) and still pause because of the collector. To write fast, stable server applications, you need a clear model of where values live, when an object becomes garbage, how modern collectors such as G1 and ZGC work, and which tools show you what's happening. This lesson builds that model with runnable experiments. The next lesson, JVM Architecture and Class Loading, zooms out to the rest of the JVM.

Stack and heap

The JVM divides memory into several areas. Two matter most in everyday code:

flowchart LR
    subgraph Stack["Stack (one frame per method call)"]
        M["main: list, count"]
    end
    subgraph Heap["Heap (shared, garbage collected)"]
        O1["ArrayList object"]
        O2["String 'Ada'"]
        O3["String 'Grace'"]
    end
    M -->|"list"| O1
    O1 --> O2
    O1 --> O3

Local variables live in stack frames; objects live on the heap. Variables hold references to objects, and the garbage collector frees heap objects that nothing refers to any more.

  • The stack. Every thread has its own stack, made of one frame per method call that hasn't returned yet. A frame holds that call's local variables and parameters. Primitives (int, double, boolean, ...) are stored directly in the frame; for objects, the frame stores only a reference (a pointer-like value). When a method returns, its frame disappears instantly, so stack memory needs no garbage collector.
  • The heap. Every object and array lives on the heap, shared by all threads. Objects stay there as long as something references them, and the garbage collector removes them afterwards. Fields of an object, including primitive fields, live inside the object on the heap.
Java
void main() {
    int count = 3;                       // stack: the value 3 itself
    String label = "total";              // stack: a reference; the String is on the heap
    int[] scores = {90, 75, 88};         // stack: a reference; the array object is on the heap
    var point = new Point(2, 5);         // stack: a reference; the Point (with x and y) is on the heap
}

This explains Java's pass-by-value behavior (see Parameters and Return Values): passing an object to a method copies the reference, so both variables point to the same heap object. It also explains why deep recursion fails: every call adds a frame, and when the stack is full you get a StackOverflowError, even if the heap is nearly empty.

Java Stack frames, references and the heap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The exact recursion depth depends on the stack size (the JVM flag -Xss, 8 MB on our server) and on how big each frame is. Catching StackOverflowError is fine for a demo, but in real code fix the recursion instead (see Recursion).

The other memory areas

Besides the heap and the thread stacks, a JVM process also uses Metaspace (class metadata such as method bytecode and field layouts, in native memory), the code cache (machine code produced by the JIT compiler), thread stacks for every thread, GC bookkeeping structures and direct buffers (ByteBuffer.allocateDirect, used by NIO). That's why a JVM with -Xmx512m can use noticeably more than 512 MB of RAM in total. The next lesson describes these areas in more detail.

Reachability: when an object becomes garbage

The collector doesn't count references. Instead, it starts from a set of GC roots and follows every reference it can find. Every object it reaches is live; everything else is garbage, no matter how many references garbage objects hold to each other. GC roots include:

  • local variables and parameters in the active frames of every thread,
  • static fields of loaded classes,
  • active threads themselves, and
  • references held by native code (JNI) and a few JVM internals.

Two consequences surprise people. First, cycles are not a problem: two objects that reference each other but are unreachable from any root are collected together (pure reference counting would leak them). Second, you rarely need obj = null. A local variable stops mattering when the method returns, so nulling it at the end of a method does nothing useful. Nulling helps only for long-lived references: a field, a static, an element of an array that you keep using.

You can watch reachability with a WeakReference: it points to an object without keeping it alive, and get() returns null once the object has been collected. System.gc() asks the JVM to collect; it's only a hint, but on our server's JVM it reliably runs a full collection, which makes it handy for experiments (don't call it in production code).

Java Unreachable objects, even in cycles, get collected

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Here nulling the locals is meaningful, because main keeps running afterwards. Even though a and b still point at each other, nothing reachable points at them, so both are collected. The object referenced from the static field survives every collection.

How generational garbage collection works

Measurements on real programs show a strong pattern, the weak generational hypothesis: most objects die young. Iterators, temporary strings, lambdas, boxed numbers and request objects are garbage within microseconds; a few objects (caches, configuration, connection pools) live for the whole run. Collectors exploit this by splitting the heap into generations:

  1. Young generation. New objects are allocated in an area called Eden. Allocation is extremely cheap: each thread bumps a pointer in its own buffer (a TLAB). When Eden fills up, a minor GC copies the few surviving objects into a survivor space and wipes Eden in one go. Its cost depends on the number of live objects, not dead ones, so lots of short-lived garbage is cheap.
  2. Old generation. Objects that survive several minor collections are promoted to the old generation. It's collected less often, by a more expensive major (or mixed/full) collection.

Most collectors also compact memory, sliding live objects together so the free space is one contiguous block. That's why Java allocation stays fast after hours of running, without the fragmentation problems of malloc.

Every collector must sometimes pause application threads, a stop-the-world pause, at least briefly, to find roots safely. Modern collectors do most of their work concurrently, while your threads keep running, so pauses stay short.

The collectors

The HotSpot JVM ships several collectors. You pick one with a command-line flag:

Collector Flag Design Best for
G1 (Garbage-First) -XX:+UseG1GC (default on most machines) Heap split into equal regions; collects the regions with the most garbage first; aims for a pause-time goal (-XX:MaxGCPauseMillis, default 200 ms) General-purpose server and desktop apps
ZGC -XX:+UseZGC Almost everything concurrent, including compaction; pauses typically well under 1 ms regardless of heap size; generational since Java 21 (the only mode since Java 24) Latency-sensitive services, very large heaps
Shenandoah -XX:+UseShenandoahGC Concurrent compaction with low pauses (in most OpenJDK builds) Similar goals to ZGC
Parallel -XX:+UseParallelGC Stop-the-world, many threads; highest throughput Batch jobs where pauses don't matter
Serial -XX:+UseSerialGC Stop-the-world, one thread; tiny overhead Small heaps, single-CPU containers, short programs
Epsilon -XX:+UseEpsilonGC (experimental) Never collects at all Performance testing, extremely short jobs

The JVM chooses G1 by default, except on small machines (one CPU or under about 2 GB of memory), where it picks Serial (up to Java 26; Java 27 makes G1 the default everywhere). The heap size is set with -Xms (initial) and -Xmx (maximum); without them, the maximum defaults to a quarter of the machine's (or container's) memory. Let's see what our server's JVM is using:

Java Heap size, memory pools and collectors at runtime

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Our server runs a deliberately small JVM: one CPU and a 256 MB heap (maxMemory() reports slightly less, because one survivor space is always kept empty) with the Serial collector, whose young collector is named "Copy" and whose old collector is "MarkSweepCompact". On your laptop the same program most likely prints "G1 Young Generation", "G1 Concurrent GC" and "G1 Old Generation", with a bigger heap. In production, -Xlog:gc prints one line per collection with its pause time; it's the first thing to enable when you suspect GC problems.

The retired collector: CMS

Older tutorials mention CMS (Concurrent Mark-Sweep, -XX:+UseConcMarkSweepGC). It was the low-pause collector before G1: it marked and swept the old generation while the application kept running, and paused only briefly for the start and end of a cycle. Its weaknesses were no compaction (the old generation became fragmented, and a failure forced a long full stop-the-world collection), extra CPU for the concurrent threads, and floating garbage that waited for the next cycle. G1 and ZGC replaced it: it was deprecated in Java 9 and removed in Java 14, so the flag no longer starts the JVM. If you meet it in a legacy configuration, switch to G1 (-XX:+UseG1GC) or, for lower pauses, ZGC.

Memory leaks in Java

A Java memory leak isn't forgotten free calls. It's references you keep by accident, so objects stay reachable and the GC is not allowed to remove them. The heap fills up slowly over hours or days, collections become more frequent and less effective, and finally the JVM throws OutOfMemoryError: Java heap space. The usual suspects:

  • Ever-growing collections in static fields or long-lived objects: caches without a size limit, "recent events" lists, maps keyed by session or request ID that nobody cleans up.
  • Listeners and callbacks that are registered but never removed. The event source keeps the listener alive, and the listener (often a lambda or inner class) keeps its whole enclosing object alive.
  • ThreadLocal values in thread pools: pool threads live forever, so values set and never remove()d live forever too.
  • Mutable HashMap keys whose hashCode changes, so entries can no longer be found or removed (see How HashMap Works Inside).
  • Unclosed resources (streams, connections) that hold buffers and native memory; use try-with-resources.

The fix is always the same idea: give long-lived references a bounded size or a clear end of life. The demo compares an unbounded cache with a bounded LRU cache built on LinkedHashMap (from LinkedHashMap and LinkedHashSet):

Java An unbounded cache leaks, a bounded one doesn't

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The bounded cache stays at 20 entries, about 20 MB, however many requests arrive. The unbounded one grows by 1 MB per request (its totals also include the bounded cache's 20 MB, which is still alive) until the 256 MB heap is full, a little over 200 reports in. In a real server the same leak would take days instead of milliseconds, which is what makes leaks so treacherous. Catching OutOfMemoryError is only acceptable in a demo like this; in production, let the JVM fail, capture a heap dump (-XX:+HeapDumpOnOutOfMemoryError) and analyze it. For real caches, use a library with size and time limits, such as Caffeine.

How to avoid memory leaks

Leaks come from reachability, so every fix is about letting go of references at the right moment:

  1. Bound every cache. Give it a maximum size and an expiry time (Caffeine, or a LinkedHashMap with removeEldestEntry), as the bounded cache above does.
  2. Remove what you register. Pair each addListener with a removeListener, and each subscription with a cancellation, in the same class that registered it.
  3. Narrow the scope. Keep data in local variables or small objects instead of static fields; set a field to null when a long-lived object no longer needs the value.
  4. Clean ThreadLocals in a finally block with remove().
  5. Use weak references (WeakHashMap, WeakReference, see below) for "extra information about an object" maps that should not keep the object alive.
  6. Close resources with try-with-resources, and keep keys of hash-based collections immutable.
  7. Test for it. Run a load test with -Xlog:gc, watch the heap after each full collection (if the low point keeps rising, something is retained), and open a heap dump to see which object holds the others.

Remember that unlike C, where "allocation and deallocation" are two calls you must pair, Java handles deallocation for you, so a leak always means "something still points at it", never "I forgot to free".

Soft, weak and phantom references

java.lang.ref offers references that the collector treats specially. A normal reference is a strong reference; the others are progressively weaker:

Kind Cleared when Typical use
Strong (normal) Never while reachable Everything
SoftReference<T> The JVM is running low on memory (all soft-only objects are cleared before an OutOfMemoryError) Memory-sensitive caches (with care)
WeakReference<T> At the next GC after the object is no longer strongly or softly reachable Metadata attached to objects you don't own: WeakHashMap, canonicalizing maps, listener registries
PhantomReference<T> After the object is finalized and collected; get() always returns null Cleanup after collection, via a ReferenceQueue or Cleaner

WeakHashMap holds its keys weakly: once a key object is unreachable elsewhere, its entry disappears automatically. java.lang.ref.Cleaner (Java 9+) runs a cleanup action after an object becomes unreachable, replacing the deprecated finalize() method. Cleaners are a safety net for native resources; explicit close() is still the primary mechanism.

The experiment below fills a soft-reference cache with forty 10 MB images, 400 MB in total, on a 256 MB heap. Strong references would crash with OutOfMemoryError; soft references let the GC throw old entries away instead. It also shows WeakHashMap and Cleaner reacting to unreachable keys:

Java Soft and weak references under memory pressure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Soft references make tempting caches, but they're hard to tune: the JVM decides when to clear them, so under memory pressure the cache can drop everything at once and trigger extra full collections. Prefer an explicitly bounded cache, and keep soft references for cases where "keep it if memory allows" is truly what you want.

Practical tips

  • Measure before tuning. Most applications run well with the default collector and an appropriate -Xmx. Enable -Xlog:gc* to see collection frequency and pause times before you change anything.
  • Size the heap for the live data. A heap two to three times the live data set gives the collector room to work; a heap that's nearly full causes back-to-back collections.
  • In containers, size relative to the container. Modern JVMs read container limits; -XX:MaxRAMPercentage=75 is a common setting.
  • Choose the collector by goal: G1 for balanced defaults, ZGC for consistently tiny pauses, Parallel for maximum batch throughput.
  • Use the tools. jcmd <pid> GC.heap_info and jcmd <pid> GC.class_histogram show what's on the heap, jcmd <pid> GC.heap_dump file.hprof writes a heap dump, JDK Flight Recorder (-XX:StartFlightRecording) with JDK Mission Control records allocations and GC events, and tools like Eclipse MAT or VisualVM find what keeps leaked objects alive.
  • Allocate less in hot paths only when profiling shows it matters: reuse StringBuilders in tight loops, avoid unnecessary boxing (see Wrapper Classes), and prefer primitive arrays or streams (IntStream) for large numeric data. Java Performance Tips covers measurement properly.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In void m() { int n = 5; StringBuilder sb = new StringBuilder(); }, where do n, the reference sb and the StringBuilder object live?

    Local variables, including object references, live in the method's stack frame. The object created with new always lives on the heap.
  2. 2. Objects A and B reference each other, and nothing else references either of them. What does the garbage collector do?

    Java's collectors trace reachability from GC roots instead of counting references, so unreachable cycles are collected like any other garbage.
  3. 3. A web server stores every request's data in a static HashMap keyed by request ID and never removes entries. What is the most likely result after running for days?

    The GC only removes unreachable objects. A static map is a GC root, so everything in it stays alive. This is the classic Java memory leak; bound the map or remove entries.
  4. 4. Why is a minor (young generation) GC usually fast even when millions of objects were allocated?

    Most objects die young. A copying young collector copies the live survivors and then reuses all of Eden, so it never has to visit dead objects one by one.
  5. 5. What does WeakHashMap do with an entry whose key is no longer referenced anywhere else?

    WeakHashMap holds keys through weak references. Once the key is only weakly reachable, the GC clears it and the map drops the entry.

Summary

  • Each thread's stack holds frames with local primitives and references; the shared heap holds every object and array. Frames vanish on return; heap objects are reclaimed by the GC.
  • An object is garbage when it's unreachable from GC roots (thread stacks, static fields, active threads); cycles don't matter. System.gc() is only a hint.
  • Generational GC allocates in Eden, copies survivors cheaply in minor GCs and promotes long-lived objects to the old generation; most collectors compact memory.
  • G1 is the default, ZGC offers sub-millisecond pauses, Parallel maximizes throughput and Serial suits tiny heaps. Size the heap with -Xms/-Xmx and watch it with -Xlog:gc, Runtime and the management beans.
  • Java leaks are unintended references: unbounded caches, unremoved listeners, ThreadLocals and unclosed resources. Bound them and analyze heap dumps when memory grows.
  • SoftReference (cleared under memory pressure), WeakReference/WeakHashMap (cleared when unreachable) and Cleaner (post-collection cleanup) cover special cases.

Next: JVM Architecture and Class Loading, where you'll see how classes are loaded, linked and turned into fast machine code.