Memory

Zero allocation is not the goal

Predictability matters more than turning an optimisation technique into a religion.

Published · updated · ~1 min read · Difficulty: Intermediate

Prerequisites

The useful question

“Does this code allocate?” is too small a question. The useful questions are: how much does it allocate, where, under what load, whether those allocations escape, and whether they disturb the latency distribution.

A service can allocate heavily and still meet its latency objective. Another can allocate almost nothing and still stall on locks, page faults or cache misses. The runtime has no obligation to respect slogans from conference slides.

What I optimise for

When zero allocation is justified

It is valuable in tight, repeated, latency-sensitive paths where object churn is measurable and the added complexity remains controlled. It is not a universal architecture principle for every request handler and DTO in the building.

Rule: remove allocation because measurement shows it is part of the problem, not because the phrase looks good in a pull request.

Evidence

Performance Lab does not currently host a benchmarked lab for allocation/GC behaviour, so the guidance above is experience-based rather than measured data on this site. Treat it as a starting hypothesis to verify with your own allocation profiling — async-profiler's allocation mode, JFR's jdk.ObjectAllocationSample event, or GC logs — not as evidence in itself.

Sources and further reading