Zero Allocation in Java: what it is and why it matters

Zero Allocation — is an approach to writing code where no unnecessary objects are created in heap memory during execution (runtime).

The main idea: fewer objects → less GC → higher stability and performance.

⚡ 1. Simple Explanation

Every time an object is created in Java:

  • memory is allocated in heap
  • GC must clean this up later
  • pressure arises on memory and CPU

Zero allocation tries to completely avoid this in the hot path.


🔥 2. Example: allocations

❌ Bad:


String[] parts = line.split(",");
  • an array is created
  • strings are created
  • regex works

✔️ Better:


// manual parsing through indexes
int start = 0;
for (int i = 0; i < line.length(); i++) {
    if (line.charAt(i) == ',') {
        // processed without creating objects
        start = i + 1;
    }
}

3. Where hidden allocations occur

1. Collections


List<Integer> list = new ArrayList<>();
  • Integer = boxing
  • each element = object

✔️ zero allocation option:


int[] arr = new int[1000];

2. Streams


list.stream().map(...).collect(...);
  • pipeline objects
  • lambda capture
  • iterable wrappers

❌ almost never zero allocation


3. String operations


"a" + "b" + "c"
  • new Strings are created
  • StringBuilder inside

4. Exceptions

  • creation of Exception object
  • stack trace allocation

⚙️ 4. Where Zero Allocation is Really Important

🔥 Only in the hot path:
  • high-load backend
  • financial systems
  • stream processing
  • real-time systems

If the code is called rarely — zero allocation is not critical.


🚀 5. How Zero Allocation is Achieved

  • using primitive types (int, long)
  • avoiding boxing (Integer, Long)
  • manual array handling
  • reusing objects (object pooling)
  • avoid streams in hot path

🧠 6. Important point about JVM

Zero allocation ≠ magic.

JVM can:
  • escape analysis remove object
  • inline methods
  • optimize temporary objects

But this is not guaranteed.


💣 7. Where Zero Allocation Breaks

  • Stream API
  • boxing/unboxing
  • String operations
  • lambda closures
  • exceptions

📊 8. Comparison of Approaches

Approach Allocations GC Load Speed Zero Allocation
for + int[] ❌ minimal 🟢 almost none 🔥 maximum ✔️ yes
List 💣 a lot 🔴 high 🟡 medium ❌ no
Stream API ⚠️ medium 🟡 medium 🟡 medium ❌ almost never
String concat ⚠️ medium 🟡 medium 🟡 medium ❌ no

🏁 Summary

Zero Allocation is not about "forbidding objects altogether".

It's about controlling the hot path and minimizing GC pressure.

Main rule:

The fewer objects → the less GC → the more stable latency

🌐 На русском
Total Likes:0

Оставить комментарий

My social media channel
By sending an email, you agree to the terms of the privacy policy

Useful Articles:

Go vs. Java - Comparing Memory Models - Part 2: Atomic Operations, Preemption, Defer/Finally, Context, Escape Analysis, GC, False Sharing
Atomic operations Atomic operations ensure correct execution of variable operations without race conditions, guaranteeing a happens-before between reads and writes. Go example: import "sync/atomic" va...
Java under the Microscope: Stack, Heap, and GC using Code Examples
Diagram - Java Memory Model - Heap / Non-Heap / Stack Heap (memory for objects) Creates objects via new. Young Generation: Eden + Survivor. Old Generation: objects that have survived several GC c...
Современный подход к параллелизму в Java - Fork/Join Framework, CompletableFuture и виртуальные потоки (Project Loom)
```html id="b6v9kc" ``` Virtual threads are especially useful for high-volume I/O operations—for example, when processing HTTP requests or accessing external APIs. The code remains linear and readabl...

New Articles:

Low-Level Mechanisms - Part 2 | Go ↔ Java
In this article, we have gathered key low-level mechanisms of Go that most often raise questions for developers coming from Java. We will discuss: unsafe.Pointer, struct alignment, pointer arithmetic,...
Compiler, Build, and Tooling in Go and Java: how assembly, initialization, analysis, and diagnostics are organized in two ecosystems
This article is dedicated to a general overview of how the compiler, build, and tooling practices are arranged in Go, and how to better understand them through comparison with Java. We will not delve ...
Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code where no unnecessary objects are created in heap memory during execution (runtime). The main idea: fewer objects → less GC → higher stability and perf...
Fullscreen image