Skip to content

Path 1 · lesson 3 of 4

Intermediate27 minJava 21 (OpenJDK 21.0.13, HotSpot, G1) · Spring Boot 3.5 · Micrometer 1.15

Object allocation, TLABs and escape analysis

What new costs: allocation in a TLAB, object headers and layout, the path from eden to the old generation, when the JIT removes an allocation, and how to find allocation hot spots.

What you will learn

  • Explain how a thread allocates an object without taking a lock
  • Work out the size of an object from its header, fields and alignment
  • Follow an object from eden through the survivor spaces to the old generation
  • State when escape analysis removes an allocation and when it cannot
  • Derive the time between young collections from the allocation rate
  • Find the code that allocates most with JFR allocation events

Before you start

The concept

Allocating an object in HotSpot is cheap. Each thread owns a piece of the young generation, its thread-local allocation buffer (TLAB), and new moves a pointer forward inside it by the size of the object. No lock, no search for free space. The cost of allocation is paid later and elsewhere: every allocated byte brings the next garbage collection closer.

An object is larger than its fields. It starts with a header, and its size is rounded up to a multiple of 8 bytes. The lab measures real sizes by allocating 100,000 objects of each kind and dividing the bytes the thread allocated by the count:

Program outputjava -Xmx256m Allocation.java sizes

Real JVM output
new Object()                                  16.0 bytes each
new Money()            one long               24.0 bytes each
new Point(x, y)        two ints               24.0 bytes each
new Order()            long, int, boolean     32.0 bytes each
Integer.valueOf(i + 1000)                     16.0 bytes each
Long.valueOf(i + 1000L)                       24.0 bytes each
new byte[0]                                   16.0 bytes each
new byte[1]                                   24.0 bytes each
new long[10]                                  96.0 bytes each
new ArrayList<>()      empty                  24.0 bytes each
On this JVM (64-bit, default settings) the header is 12 bytes: an 8-byte mark word and a 4-byte compressed class pointer. new Object() is 12 bytes rounded up to 16. An object with one long is 12 + 8, with padding to align the long: 24. Two int fields: 12 + 8 = 20, rounded to 24. An Integer is 16 bytes for 4 bytes of data, a Long 24 for 8. Arrays have 4 more header bytes for the length: an empty byte[] is 16 bytes, long[10] is 16 + 80 = 96. A list of a million boxed Long values costs 24 MB for the objects plus 4 MB for the references, against 8 MB for a long[].

Why it exists

A service can be slow because of allocation without ever running out of memory. The pattern has a recognisable shape: latency spikes every second or so that line up with young collections, CPU that is high while traffic is flat, and a heap graph that is a fast, regular saw-tooth. Nothing leaks. The code simply creates more temporary objects per request than it needs to, and at 2,000 requests per second a few kilobytes of waste per request is tens of megabytes per second.

  • More collections. Eden fills faster, so young collections run more often. Each one stops the application threads for a few milliseconds.
  • More promotion. Collections that come closer together give objects less time to die. More of them survive, are copied, and reach the old generation, which makes the expensive collections come sooner too.
  • Less CPU for work. Allocation itself, zeroing the memory, and the GC threads all take CPU and memory bandwidth away from request handling.

Two of the investigation cases are this problem from the outside: Spikes every few seconds and CPU pinned with flat traffic. The pause side is covered in GC tuning and pause analysis. This lesson is about the cause: who allocates, and how much.

How it works inside

TLAB allocation

The young generation is shared by all threads, so allocating directly in it would need synchronisation on every new. Instead each thread asks for a block of eden, a TLAB, and allocates inside it alone. The fast path compiled into your code is: add the object size to the thread's allocation pointer, check that it is still inside the TLAB, write the header. TLABs are on by default (-XX:+UseTLAB) and the JVM chooses and adapts their size per thread.

  • TLAB full. The thread retires the buffer and asks for a new one. This is the slow path and involves the shared heap. The unused tail of the old TLAB is wasted.
  • Object too large for the TLAB. It is allocated outside, directly in the shared heap. Large arrays take this path. With G1, very large objects get regions of their own; see G1 and ZGC.
  • Eden full. No TLAB can be handed out. The thread triggers a young collection and waits for it.

These slow paths are also where the JVM can observe allocation cheaply. JFR's allocation events are tied to them and not to every new, which is why allocation profiling with JFR is affordable in production.

Header and layout

PartSize on 64-bit HotSpot (JDK 21 defaults)What it holds
Mark word8 bytesThe identity hash code once computed, the object's age for the collector, and locking state
Class pointer4 bytes (compressed), 8 without compressionWhich class the object is an instance of
Array length4 bytes, arrays onlyThe number of elements
Fields1 to 8 bytes each; a reference is 4 bytes with compressed oopsThe JVM may reorder fields to reduce padding
Padding0 to 7 bytesRounds the object up to a multiple of 8 bytes (-XX:ObjectAlignmentInBytes)
JEP 450 describes the JDK 21 header as between 96 and 128 bits. Its 64-bit compact header arrived as an experimental option in JDK 24, not in 21.

The practical consequence is that small objects are mostly overhead. Boxed numbers, single-field wrappers, Optional, entries of collections and iterators all pay 12 to 16 bytes before the first byte of data. Collections of primitives stored as objects are where this adds up.

The life of an object

The heap is split by age because most objects die young. New objects go to eden. A young collection finds the objects in eden that are still reachable and copies them to a survivor space; everything else in eden is garbage and costs nothing to reclaim. Survivors that are still alive at the next collection are copied again, and each copy increases the object's age. When the age reaches the tenuring threshold, or the survivor space is too small to hold them, objects are copied into the old generation, where they stay until a collection of the old generation finds them dead.

GC logjava -Xmx256m -Xlog:gc,gc+age=trace Allocation.java pressure wasteful 3

Real JVM output
[0.023s][info][gc] Using G1
...
[1.888s][debug][gc,age] GC(2) Desired survivor size 5242880 bytes, new threshold 15 (max threshold 15)
[1.916s][trace][gc,age] GC(2) Age table with threshold 15 (max threshold 15)
[1.916s][trace][gc,age] GC(2) - age   1:    2753904 bytes,    2753904 total
[1.916s][trace][gc,age] GC(2) - age   2:     296840 bytes,    3050744 total
[1.916s][trace][gc,age] GC(2) - age   3:    1009256 bytes,    4060000 total
[1.916s][info ][gc    ] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 74M->5M(126M) 27.819ms
[2.481s][debug][gc,age] GC(3) Desired survivor size 5242880 bytes, new threshold 15 (max threshold 15)
[2.513s][trace][gc,age] GC(3) Age table with threshold 15 (max threshold 15)
[2.513s][trace][gc,age] GC(3) - age   1:    2772576 bytes,    2772576 total
[2.513s][trace][gc,age] GC(3) - age   2:     834056 bytes,    3606632 total
[2.513s][trace][gc,age] GC(3) - age   3:       1560 bytes,    3608192 total
[2.513s][trace][gc,age] GC(3) - age   4:     860744 bytes,    4468936 total
[2.513s][info ][gc    ] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 75M->5M(126M) 32.225ms
[2.697s][debug][gc,age] GC(4) Desired survivor size 5242880 bytes, new threshold 15 (max threshold 15)
[2.717s][trace][gc,age] GC(4) Age table with threshold 15 (max threshold 15)
[2.717s][trace][gc,age] GC(4) - age   1:    2907216 bytes,    2907216 total
[2.717s][trace][gc,age] GC(4) - age   2:     613536 bytes,    3520752 total
[2.717s][trace][gc,age] GC(4) - age   3:      85880 bytes,    3606632 total
[2.717s][trace][gc,age] GC(4) - age   4:       1560 bytes,    3608192 total
[2.717s][trace][gc,age] GC(4) - age   5:     860744 bytes,    4468936 total
[2.717s][info ][gc    ] GC(4) Pause Young (Normal) (G1 Evacuation Pause) 74M->5M(126M) 19.851ms
Each young collection prints an age table and then its summary line. Take GC(4): the heap went from 74 MB to 5 MB, so about 93% of what was allocated since the previous collection was already dead. Of the survivors, 2.9 MB had survived one collection (age 1), 0.6 MB two, and almost nothing three. In this program the survivors are the last 20,000 results, which stay referenced for a short while and then die. new threshold 15 (max threshold 15) is the tenuring threshold: an object must survive 15 collections before it is promoted for age alone. The 860,744 bytes that move from age 4 to age 5 between GC(3) and GC(4) are long-lived data on its way to the old generation.

Escape analysis and scalar replacement

The C2 compiler can remove an allocation entirely. Escape analysis checks whether a newly created object can be reached from outside the compiled method: stored in a field or a static, put in a collection, returned, or passed to code that was not inlined. If it cannot (the documentation calls this state NoEscape), C2 applies scalar replacement: the object is never created, and its fields become local variables that live in CPU registers or on the stack. Locks on such an object are removed as well.

Program outputThe same loop with escape analysis on and off

Real JVM output
$ java -Xmx256m Allocation.java escape
round 1: 20,000,000 calls,   92 ms,     2,224,544 bytes allocated
round 2: 20,000,000 calls,   77 ms,            32 bytes allocated
round 3: 20,000,000 calls,   64 ms,            32 bytes allocated
round 4: 20,000,000 calls,   57 ms,            32 bytes allocated
round 5: 20,000,000 calls,   64 ms,            32 bytes allocated
checksum 900000000, young collections 0
$ java -Xmx256m -XX:-DoEscapeAnalysis Allocation.java escape
round 1: 20,000,000 calls,  875 ms,   960,006,536 bytes allocated
round 2: 20,000,000 calls,  957 ms,   960,000,056 bytes allocated
round 3: 20,000,000 calls, 1030 ms,   960,000,056 bytes allocated
round 4: 20,000,000 calls,  984 ms,   960,000,056 bytes allocated
round 5: 20,000,000 calls,  912 ms,   960,000,056 bytes allocated
checksum 900000000, young collections 46
The loop calls distance(...) 20 million times per round, and the method creates two Point records per call. With defaults, round 1 allocates about 2 MB while the code is still interpreted and compiled by C1, and from round 2 on the round allocates 32 bytes: C2 has inlined distance and replaced both points with plain ints. There is no young collection in the whole run. With -XX:-DoEscapeAnalysis every round allocates 960,000,056 bytes, which is 20,000,000 × 2 × 24 bytes, and the run needs 46 young collections. The rounds are also more than ten times slower here, but the machine was shared, so read the byte counts, not the milliseconds.
The allocation is removed whenThe allocation stays when
The object is used only inside one method after inlining: a temporary point, an iterator of a for loop, a small builderThe object is stored in a field, an array or a collection, or returned to a caller that was not inlined
The methods it is passed to are small enough to be inlined into the hot methodIt is passed to a method that is too large, too deep or too polymorphic to inline
The method has been compiled by C2, which needs thousands of calls firstThe code runs in the interpreter or in C1: cold code, startup, rarely used paths
Control flow is simpleThe object is created on one branch and merged with another object on a different branch before use
Escape analysis is on by default. The JDK documentation is explicit that C2 does not move escaping objects to the stack: an object is either removed completely or allocated on the heap.

Escape analysis is an optimisation you can benefit from but should not design around. It is silent when it stops working: a refactoring that makes a method one bytecode too large to inline brings the allocations back, and nothing fails. The way to know is to measure allocation, which the next section does.

Architecture

Drawing diagram…

Flowchart. A new object in a hot method is either removed by scalar replacement, if C2 compiled the method and the object does not escape, or allocated. Allocation is a pointer bump in the thread's TLAB, or a new TLAB or an allocation outside it. The object is in eden. At a young collection, unreachable objects are reclaimed and reachable ones are copied to a survivor space with their age increased. Objects that reach the age threshold or do not fit are moved to the old generation.

flowchart TD
  A["new Point in a hot method"] --> B{"Compiled by C2 and does not escape"}
  B -->|"Yes"| C["Scalar replaced, no allocation"]
  B -->|"No"| D{"Fits in the thread TLAB"}
  D -->|"Yes"| E["Bump the pointer in the TLAB"]
  D -->|"No"| F["New TLAB, or allocate outside in the shared heap"]
  E --> G["Eden"]
  F --> G
  G -->|"Young collection, still reachable"| H["Survivor space, age plus one"]
  G -->|"Young collection, unreachable"| X["Reclaimed at no cost"]
  H -->|"Still alive at the next collection"| H
  H -->|"Age reaches the threshold or survivor is full"| I["Old generation"]
Where an allocation goes, from the new expression to the old generation.
  • The first decision is made by the JIT compiler, per allocation site, and only for hot code. Everything below it is the path of objects that really are allocated.
  • The TLAB fast path touches only memory the thread owns. This is why allocation scales with threads and why its direct cost is so low.
  • A young collection does work only for objects that are alive. The arrow to 'reclaimed' costs nothing, which is why a high allocation rate with a small live set gives many short pauses and not long ones.
  • The loop on the survivor space is the copying of medium-lived objects. Each pass is real work during a pause.

Step by step

Drawing diagram…

Sequence diagram. A request thread allocates objects by bumping a pointer in its TLAB. When the TLAB is full it requests a new one from eden. When eden is full the thread triggers a young collection, all application threads pause, the collector finds the reachable objects in eden and copies them to a survivor space or to the old generation, and the thread resumes with an empty eden.

sequenceDiagram
  participant T as Request thread
  participant L as Its TLAB
  participant E as Eden
  participant G as Garbage collector
  participant S as Survivor and old
  T->>L: new byte array, bump pointer
  T->>L: new String, bump pointer
  L-->>T: TLAB is full
  T->>E: Request a new TLAB
  E-->>T: New TLAB
  T->>L: Keep allocating
  T->>E: Request a new TLAB
  E-->>T: Eden is full
  T->>G: Trigger a young collection
  Note over T,G: All application threads pause
  G->>E: Find the objects that are still reachable
  G->>S: Copy them to survivor, or to old if aged
  G-->>T: Eden is empty again, resume
  T->>E: Request a new TLAB
One thread allocating until eden is full, and the young collection that follows.
  1. Most new operations are the first two arrows: they involve only the thread and its own buffer.
  2. The collection is triggered by an allocation that cannot be satisfied. The more bytes per second all threads allocate together, the sooner that moment comes.
  3. The pause is spent on the reachable objects. Its length follows the size of what survives, not the size of eden.
  4. With several threads the picture is the same per thread, and the pause stops all of them, whichever thread triggered it.

In a real system

An order service builds a text line for each order: an id, a total and a list of part prices. The first version uses the most convenient API at every step. The lab runs it in a loop as fast as one thread can, then runs a rewritten version that produces the same string.

Program outputBytes allocated per request, before and after

Real JVM output
$ java -Xmx256m Allocation.java pressure wasteful 6
wasteful: 450,000 requests in 6 s (75,000 per second)
allocated 1,880 MB (313 MB per second, 4,382 bytes per request)
collections during the run: 23
$ java -Xmx256m Allocation.java pressure lean 6
lean: 3,094,000 requests in 6 s (515,666 per second)
allocated 1,133 MB (188 MB per second, 384 bytes per request)
collections during the run: 17
The convenient version allocates 4,382 bytes per request, the rewritten one 384 bytes, for the same output. The second run handled several times as many requests in the same six seconds and still needed fewer collections (17 against 23). Requests per second depend on how busy the machine was; bytes per request do not. At 2,000 requests per second the difference is 8.8 MB/s against 0.8 MB/s for this one method.

In a real service you do not know in advance which method is the wasteful one. Java Flight Recorder samples allocations with their stack traces at low cost (the event jdk.ObjectAllocationSample, on by default in JFR's settings and limited to 150 events per second). A ten second recording of the wasteful run gives:

JFRjfr view allocation-by-class alloc.jfr

Real JVM output
                              Allocation by Class

Object Type                                                 Allocation Pressure
----------------------------------------------------------- -------------------
byte[]                                                                   50.27%
java.lang.String                                                         16.75%
java.lang.Object[]                                                       12.00%
java.util.stream.ReferencePipeline$5                                      8.51%
java.util.Formatter$FormatSpecifier                                       4.99%
java.lang.Long                                                            3.12%
java.util.stream.ReduceOps$8ReducingSink                                  2.58%
java.util.ArrayList                                                       1.02%
java.util.HashMap$KeySet                                                  0.31%
java.util.Formatter                                                       0.27%
java.util.stream.ReferencePipeline$Head                                   0.05%
Allocation Pressure is the share of sampled allocation by type. Half of everything is byte[], which is what strings and string builders are made of, then String itself and Object[] (the backing array of the ArrayList and the varargs array of String.format). ReferencePipeline and ReduceOps are the stream. java.lang.Long at 3% is the boxing. The type alone rarely tells you where to look: byte[] is at the top of this list in most services.

JFRjfr print --events jdk.ObjectAllocationSample --stack-depth 8 alloc.jfr (one event)

Real JVM output
jdk.ObjectAllocationSample {
  startTime = 21:40:18.864 (2026-11-15)
  objectClass = java.lang.String (classLoader = bootstrap)
  weight = 118.3 kB
  eventThread = "main" (javaThreadId = 1)
  stackTrace = [
    java.lang.StringConcatHelper.newString(byte[], long) line: 400
    Allocation.wasteful(long, int, long) line: 96
    Allocation.pressure(String, int) line: 118
    Allocation.main(String[]) line: 32
  ]
}
The stack trace is what finds the code. The top frame, StringConcatHelper.newString, is JDK code: the implementation of string concatenation. The first frame of your code is Allocation.wasteful ... line: 96, the line line += part + ";" inside a loop. weight is the amount of allocation this one sample stands for. The view jfr view allocation-by-site aggregates by top frame only, so in this recording it lists JDK methods (Unsafe.allocateUninitializedArray 38%, StringConcatHelper.newString 13%, Arrays.copyOf 12%) and your own method at 1%. Read the printed events, or open the recording in JDK Mission Control, to see callers.

The order of work in production is therefore: confirm from the GC log or metrics that the allocation rate is high for the traffic, record JFR for a minute under real load, find the application frames that account for most of the sampled weight, and change those few places. Then measure bytes per request again.

Java and Spring Boot

Two measurements for a Spring Boot 3.5 service: bytes allocated per HTTP request, recorded as a metric by a servlet filter, and a JFR recording taken from the running process. The filter was compiled here against stub versions of the Spring, servlet and Micrometer types, which checks the syntax and the JDK calls but not the framework wiring. It was not run inside Spring.

src/main/java/com/example/orders/AllocationPerRequestFilter.javaJava
package com.example.orders;import java.io.IOException;import java.lang.management.ManagementFactory;import io.micrometer.core.instrument.DistributionSummary;import io.micrometer.core.instrument.MeterRegistry;import jakarta.servlet.FilterChain;import jakarta.servlet.ServletException;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import org.springframework.stereotype.Component;import org.springframework.web.filter.OncePerRequestFilter;/** Records how many bytes the request thread allocated while handling each request. */@Componentpublic class AllocationPerRequestFilter extends OncePerRequestFilter {    private final com.sun.management.ThreadMXBean threads =            (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();    private final DistributionSummary allocated;    public AllocationPerRequestFilter(MeterRegistry registry) {        this.allocated = DistributionSummary.builder("http.server.request.allocated")                .description("Bytes allocated by the request thread per request")                .baseUnit("bytes")                .publishPercentiles(0.5, 0.99)                .register(registry);    }    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)            throws ServletException, IOException {        long before = threads.getCurrentThreadAllocatedBytes();        try {            chain.doFilter(request, response);        } finally {            long after = threads.getCurrentThreadAllocatedBytes();            if (before >= 0 && after >= before) {                allocated.record(after - before);            }        }    }}

Why it is written this way

  • getCurrentThreadAllocatedBytes() is a counter the JVM keeps per thread. Reading it twice costs far less than the request, so the filter can stay on in production.
  • It counts only what this thread allocates. Work handed to another executor, and asynchronous requests that finish on a different thread, are not included.
  • The counter is -1 when the JVM does not support or has disabled the measurement, hence the guard. Check what it returns on your setup if requests run on virtual threads before trusting the metric.
  • Bytes per request multiplied by requests per second is the allocation rate this endpoint contributes. A release that doubles the median is visible on a dashboard before it is visible in GC pauses.
  • For the whole JVM, Micrometer already publishes jvm.gc.memory.allocated, a counter of bytes allocated in the young generation. Its rate is the allocation rate.

Wrong, then right

The report line from the scenario, as it might be written first and as it looks after the allocation profile pointed at it. Both methods return the same string. Both were compiled and run as part of labs/jvm/extras/Allocation.java, where they are called wasteful and lean.

Incorrect implementation

src/main/java/com/example/orders/OrderReportService.java (4,382 bytes per call)Java
package com.example.orders;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Service;@Servicepublic class OrderReportService {    /** One line per order: id, total and the price of each part. Called for every order on every export. */    public String line(long orderId, int quantity, long priceCents) {        List<Long> parts = new ArrayList<>();        for (int i = 0; i < quantity; i++) {            parts.add(priceCents + i);        }        long total = parts.stream().mapToLong(Long::longValue).sum();        String line = "";        for (Long part : parts) {            line += part + ";";        }        return String.format("order=%d total=%d parts=%s", orderId, total, line);    }}

Why it is written this way

  • List<Long> boxes every price: a 24-byte Long object per element, plus an ArrayList that starts empty and re-allocates its backing array as it grows.
  • The stream creates pipeline and sink objects on every call to add up 20 numbers.
  • line += part + ";" in a loop builds a new String and a new byte[] on every iteration, each one a copy of everything so far plus one element.
  • String.format parses the format string on every call and boxes its arguments into an Object[].

Why it fails

  • Every call allocates about 4.4 KB of objects that are dead by the time it returns. None of them can be removed by escape analysis: they are stored in a collection, handed to stream and formatter code that is too large to inline, or copied into the next string.
  • At 2,000 exports per second this one method allocates almost 9 MB per second. It fills eden that much faster, so young collections come more often, and under load some of the half-built strings survive a collection and are copied.
  • The symptom in production is not an error. It is a GC log with a young collection several times per second, jvm.gc.memory.allocated rising faster than traffic explains, and a JFR allocation view dominated by byte[] and String with this method in the stack traces.

Corrected implementation

src/main/java/com/example/orders/OrderReportService.java (384 bytes per call)Java
package com.example.orders;import org.springframework.stereotype.Service;@Servicepublic class OrderReportService {    /** One line per order: id, total and the price of each part. Called for every order on every export. */    public String line(long orderId, int quantity, long priceCents) {        long total = 0;        for (int i = 0; i < quantity; i++) {            total += priceCents + i;        }        StringBuilder line = new StringBuilder(48 + quantity * 6);        line.append("order=").append(orderId).append(" total=").append(total).append(" parts=");        for (int i = 0; i < quantity; i++) {            line.append(priceCents + i).append(';');        }        return line.toString();    }}

Why it is written this way

  • The total is computed with primitives. Nothing is allocated for it.
  • One StringBuilder, sized once from the expected length, so its internal array is not re-allocated while appending.
  • append(long) writes the digits straight into the builder. No intermediate String per number.
  • What remains is the builder's array and the final String with its own array: 384 bytes for a line of about 150 characters. That part is the result and has to exist.

Trade-offs

  • The second version is less declarative. The format of the line is spread over several append calls where the first version had one format string. For code that runs a few times per minute, the first version is the better choice.
  • The capacity estimate (48 + quantity * 6) is a guess about the data. If prices get longer the builder still works but re-allocates once; if the guess is far too large, memory is wasted on every call.
  • This kind of rewrite is only worth doing where a profile shows the allocation. Applied everywhere it costs readability and gains nothing, because most code is not hot.
  • It does not help if the real problem is the live set. A service whose old generation is full of cached data gains little from allocating less per request.

Configuration

Allocation needs no tuning in most services. These are the flags and settings that show it or, rarely, change it. Defaults are from the JDK 21 java command reference and the JDK's own default.jfc.

-XX:+UseTLABDefault enabled
What it does
Threads allocate in thread-local allocation buffers in the young generation.
When to change it
Leave it on. Disabling it makes every allocation go through the shared heap.
-XX:TLABSizeDefault 0 (the JVM selects the size and adapts it)
What it does
Initial size of a TLAB in bytes.
When to change it
Leave it at 0. Fixed sizes rarely beat the adaptive default and waste eden when there are many threads.
-XX:+DoEscapeAnalysisDefault enabled
What it does
C2 analyses whether new objects escape and removes allocations that do not.
When to change it
Leave it on. -XX:-DoEscapeAnalysis is useful only to see, in a test, how much allocation it was removing.
-XX:MaxTenuringThresholdDefault 15
What it does
The highest number of young collections an object can survive before it is moved to the old generation. The collector may use a lower threshold when the survivor space is tight.
When to change it
Rarely worth changing. If the age table shows objects promoted young, the cause is usually a survivor space that is too small or an allocation rate that is too high.
-Xlog:gc+age=traceDefault off
What it does
Prints the age table at every young collection: bytes surviving at each age and the current threshold.
When to change it
Enable it when promotion is suspected. Large amounts at high ages mean medium-lived objects are being copied many times.
jdk.ObjectAllocationSample (JFR event)Default enabled, throttled to 150 events per second
What it does
Sampled allocations with type, weight and stack trace.
When to change it
The default is right for production. Raise the throttle only for short recordings in a test environment.

Hands-on exercise

Allocation rate against live set

Use the heap simulation to see which of the two numbers controls how often collections happen and which controls how bad they are. Then confirm one data point on a real JVM.

Steps · 0 of 6 done

A generational heap under allocation

Simulation · not a real JVM

Change the allocation rate and the live set one at a time. Watch how often young collections happen, how long they pause, and when other kinds of collection appear.

Take it further in the simulation: GC pressure: when the live set fills the heap

What can go wrong

Looking for a leak when the problem is the rate
Symptom
Frequent young collections and latency spikes. Heap dumps show nothing unusual, and heap after GC is flat.
Cause
The live set is fine. The service allocates many megabytes per second of objects that die at once, and a heap dump only shows what is alive.
Fix
Measure allocation, not retention: allocation rate from the GC log or jvm.gc.memory.allocated, then JFR allocation samples to find the code.
Reading the allocation view by top frame
Symptom
jfr view allocation-by-site lists Arrays.copyOf, StringConcatHelper.newString and other JDK methods. The team concludes the JDK is the problem or that nothing can be done.
Cause
The view groups by the frame that performed the allocation, which is almost always library code called from yours.
Fix
Print the events with stack traces (jfr print --events jdk.ObjectAllocationSample --stack-depth 12) or use JDK Mission Control, and find the first frame in your own packages.
Counting on escape analysis
Symptom
A method that 'allocates nothing' in a microbenchmark allocates heavily in the service, or starts to after an unrelated refactoring.
Cause
Scalar replacement needs C2 and needs every use of the object to be inlined. A larger method, a new implementation of an interface or a different call path can stop the inlining, and the allocation returns without any warning.
Fix
Treat it as a bonus. Where allocation matters, measure bytes per request in the real service and after every significant change.
Pooling small objects to 'help the GC'
Symptom
Object pools for small, short-lived objects make the code harder to follow, and pauses get longer, not shorter.
Cause
Pooled objects live long, so they are copied through the survivor spaces into the old generation, and the pool itself needs synchronisation. Dying young in a TLAB was the cheap case.
Fix
Pool only what is expensive to create (connections, large buffers, threads). For small objects, allocate less or allocate nothing; do not allocate once and keep.
Boxed numbers in large collections
Symptom
A Map<Long, Long> or List<Integer> with millions of entries uses several times the memory expected, and collections of the old generation take long.
Cause
Every boxed value is a separate object of 16 or 24 bytes with its own header, plus a reference to it, plus the entry object of the map.
Fix
Use primitive arrays, or a primitive collections library, for large numeric data. Keep boxed collections for small or cold data.

How to troubleshoot

Young collections several times per second

Check

-Xlog:gc*:file=gc.log   then compare consecutive lines such as `Pause Young (Normal) ... 74M->5M(126M)` and their timestamps
How to read it
Heap before this collection minus heap after the previous one is what was allocated in between. Divide by the time between the two lines to get the allocation rate. 70 MB every 150 ms is about 470 MB per second.
Need the allocation rate of a running service without touching it

Check

Micrometer `jvm.gc.memory.allocated` (a counter in bytes; take its rate per second)
How to read it
Divide the rate by requests per second to get bytes per request, and compare that figure between releases and between endpoints. A jump after a deploy is a regression to profile.
Allocation rate is high and you do not know which code is responsible

Check

jcmd <pid> JFR.start name=alloc duration=60s filename=/tmp/alloc.jfr   then   jfr print --events jdk.ObjectAllocationSample --stack-depth 12 /tmp/alloc.jfr
How to read it
Each event has a type, a weight and a stack trace. The application frames that appear in most of the sampled weight are the allocation hot spots.
Old generation grows steadily although nothing is cached

Check

-Xlog:gc+age=trace   and look at the bytes at high ages and at the `new threshold` value
How to read it
Large amounts at ages above 2 or 3, or a threshold that the collector has lowered far below 15, mean medium-lived objects are being promoted. Find what keeps request state alive for seconds.
One thread pool is suspected of allocating most

Check

jfr view allocation-by-thread recording.jfr   (or `jfr view thread-allocation recording.jfr` for totals per thread)
How to read it
Allocation concentrated in one pool points at the work that pool does: a serialiser, a scheduled job, a batch import.

Key takeaways

  • Allocation is a pointer bump in a thread-local buffer. It is cheap to do and costs later, by bringing the next collection closer.
  • Time between young collections is about eden size divided by allocation rate. Pause length follows what survives, not what was allocated.
  • Every object has a 12 to 16 byte header and is padded to 8 bytes. Small objects and boxed numbers are mostly overhead.
  • Objects that die before the next young collection are nearly free. Objects that live for a few seconds are copied repeatedly and may be promoted.
  • C2 removes allocations of objects that do not escape a hot, inlined method. It is on by default, silent, and not something to rely on.
  • Find allocation hot spots with JFR's jdk.ObjectAllocationSample events and read the stack traces down to your own code.
  • Track bytes allocated per request. It is stable, comparable between releases, and independent of how busy the machine is.

Practical check

Small scenarios, one decision each. Pick what you would do, then read why.

Scenario 1 of 3

A service handles 1,500 requests per second. The GC log shows a young collection every 180 ms, each taking the heap from about 410 MB to 90 MB, with pauses of 6 ms. Heap after GC has been flat at 90 MB for days. p99 latency shows a small spike several times per second.

What is the right reading of these numbers?

Scenario 2 of 3

A hot method creates a small Money object, passes it to two short private helpers and returns a long. Measured in the running service it allocates 0 bytes per call. A colleague adds one line that stores the object in an instance field, this.lastPrice = money, for a debug endpoint. The service's allocation rate rises by 30 MB per second.

Why did the allocation come back?

Scenario 3 of 3

A JFR recording of a busy service shows in allocation-by-class: byte[] 46%, java.lang.String 19%, java.lang.Object[] 9%. In allocation-by-site the top entries are Arrays.copyOf, StringConcatHelper.newString and AbstractStringBuilder.<init>.

What do you do next?

References

References: JDK 21 HotSpot Virtual Machine Guide: Performance Enhancements (escape analysis, scalar replacement, compressed oops) ↗ · JDK 21 Garbage Collection Tuning Guide: Garbage Collector Implementation (generations, eden, survivor spaces, aging) ↗ · JDK 21: the java command (-XX:+UseTLAB, -XX:TLABSize, -XX:+DoEscapeAnalysis, -XX:MaxTenuringThreshold) ↗ · JEP 450: Compact Object Headers (describes the 96 to 128 bit header of earlier releases) ↗ · JDK 21: the jfr command (print, view, summary) ↗ · Aleksey Shipilëv, JVM Anatomy Quark 4: TLAB allocation ↗ · Micrometer source: JvmGcMetrics (jvm.gc.memory.allocated) ↗ · Report a problem