Skip to content
javahacks.dev
JVM & Performance

How to Reduce Allocation Rates in Hot Paths in Java

Reduce Java allocation rates in hot code: measure with JFR and JMH, avoid boxing and temporary strings, reuse buffers, use primitives and know when to stop.

6 min read
Summarize in AI
Flat illustration of items passing through stream filter, map and collect stages, with the OpenJDK, Apache Spark, ReactiveX and Kotlin logos.

Allocation in Java is cheap, and it is also a cost that grows with volume. Creating an object means bumping a pointer, and discarding it is nearly free for a young-generation collector. The bill arrives when a hot path creates millions of short-lived objects per second: the young generation fills quickly, collections run often, cache lines are churned, and under pressure objects get promoted to the old generation where they cost far more to clear. Reducing allocation can cut latency and CPU in the right place and wastes time in the wrong one. This post shows how to measure first, then the usual sources in order of payoff, then the point at which to stop. It uses JDK 25 and G1.

Measure

Do not guess where the allocations come from. Java Flight Recorder samples allocations with their stack traces at low overhead. Record a minute of representative load:

Shell
jcmd <pid> JFR.start name=alloc settings=profile duration=60s filename=/tmp/alloc.jfr
jfr print --events jdk.ObjectAllocationSample /tmp/alloc.jfr | head -80

Open the file in JDK Mission Control and look at the Memory view, "Allocation by Class" and "Allocation by Site", sorted by weight. The jdk.ObjectAllocationSample event records the allocated type and the stack, weighted by the bytes allocated, so the top entries are the methods creating the most garbage. In a microbenchmark, JMH's GC profiler (-prof gc) reports allocated bytes per operation, which is the number to track: gc.alloc.rate.norm in B/op. How to write such a benchmark without fooling yourself is in writing correct JMH benchmarks and benchmarking mistakes.

Check the effect on the collector at the same time. The GC log shows how often young collections run and how long they take; a rate of one collection per second with short pauses is not a problem, while ten per second with growing pauses might be. Reading GC logs explains the fields. If allocation is not behind a measured problem, stop here.

Boxing

The most common hidden allocation is boxing a primitive. A Map<Integer, Long> allocates an Integer key and a Long value for each entry outside the small-value cache, and a List<Integer> of a million numbers is a million objects plus an array of references.

src/main/java/com/example/Stats.javajava
// allocates a Long per element
long total = values.stream().mapToLong(Long::longValue).sum();

// no boxing at all
long total = 0;
for (long v : primitiveValues) total += v;

Use primitive arrays, and the primitive streams IntStream, LongStream and DoubleStream, when processing numbers. For primitive-keyed maps, libraries such as Eclipse Collections, fastutil or HPPC provide LongLongHashMap types with no boxing. In generic code you cannot avoid boxing until Valhalla's value classes arrive; check the status of that work in your JDK before designing around it. Lambdas that capture variables allocate an object per creation, and non-capturing lambdas are reused. A stream pipeline allocates the stream objects, the lambdas and the intermediate boxed values; in a hot loop that runs millions of times, a plain loop avoids all of it. Keep streams for the code where readability matters more than a few hundred bytes per call.

Strings

Strings are the next largest source in most services. Concatenation in a loop creates a new string per step. Since JDK 9 the compiler uses invokedynamic string concatenation, which is efficient for a single expression, and a loop that appends in pieces to a String variable is still quadratic. Use a StringBuilder with a sensible initial capacity for building text in loops:

java
var sb = new StringBuilder(64 + items.size() * 24);
for (Item i : items) sb.append(i.sku()).append(',').append(i.quantity()).append('\n');

String.format is expensive: it parses the format string on each call and allocates formatters; in a hot path replace it with concatenation or an append. substring copies since JDK 7; split allocates an array and strings, and a regular expression allocates matchers, so for a single-character separator on a hot path write a small scanner or use indexOf. Logging is a frequent offender: log.debug("order " + id + " total " + total) builds the message even when debug is off. Use parameterised messages, log.debug("order {} total {}", id, total), which format only if enabled. Compact strings (Latin-1 storage, on by default since JDK 9) already halve the memory for most text.

Reuse buffers

Allocating a new 8 KB byte array per request, per read or per message is a classic cost, for example in a custom protocol reader. Reuse the buffer within the scope where it is safe: a field of a single-threaded component, or a ThreadLocal for pooled thread use. Virtual threads make a ThreadLocal buffer wasteful, since there may be a million threads; use ScopedValue for context, or a small pool of buffers with explicit acquire and release, as covered in ScopedValue versus ThreadLocal. Libraries that already pool for you, such as Netty's pooled allocator, should be configured and not bypassed. Buffer reuse brings the risk of data leaking between uses and of retaining a large buffer forever, so clear what must be cleared and cap sizes.

Avoid temporary objects

Look for objects created only to be used and dropped within a call: a Pair for returning two values, an Optional wrapped around a value you immediately unwrap, an iterator over a list where an indexed loop would do, a new int[1] as a mutable holder for a lambda. The JIT's escape analysis can eliminate some allocations entirely, by scalar replacement, when the object does not escape the method after inlining. That makes micro-optimising by hand risky: code you "fixed" may already have been optimised, and the benchmark shows nothing. Check with JMH rather than assuming, and be aware of the traps described in escape analysis and misleading benchmarks in the earlier cluster.

Return values can be restructured: a method returning a small record that is inlined may allocate nothing, while one that is too large to inline will. Prefer returning primitives or writing into a caller-provided structure only where a profile demands it, because the readable code wins when the difference does not show.

Know when to stop

The goal is to meet a measured target, such as p99 latency or CPU per request, not to reach zero allocation. A service that allocates 500 MB per second on a modern collector may be perfectly healthy. Stop when the allocation profile no longer has a dominant site, when the remaining work would hurt readability for a gain you cannot measure, or when latency is limited by something other than the collector (a database, a lock). Allocation-free code is harder to write, to review and to change, and each trick should have a benchmark number beside it in a comment.

Check the GC effect

Verify the end-to-end change, and look beyond the allocation counter. After a change, compare young-collection frequency, pause times, and request latency percentiles under the same load. G1 usually adapts to a lower allocation rate with shorter, less frequent pauses. Sometimes tuning the heap and region sizes is cheaper than code changes; see tuning G1 without cargo culting. A larger young generation can absorb a high allocation rate at the price of memory.

Benchmark properly

Use JMH, with warm-up, forks and Blackhole to defeat dead-code elimination. A naive loop timing with System.nanoTime measures the interpreter, the compiler warming up and the garbage collector, in unknown proportions. Report allocation (B/op) along with time (ns/op), because a change that is slightly slower and allocates a tenth as much may be a net win under real load where the collector competes for the same cores.

When this does not apply

If the application is waiting on I/O most of the time, allocation is not the bottleneck, and effort belongs elsewhere. The GC tuning guide is at docs.oracle.com/en/java/javase/25/gctuning, and the JFR API at docs.oracle.com/en/java/javase/25/jfapi.

Taggedhow to
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.