Table of Contents:
- ⚡ 1. Simple explanation
- 🔥 2. Example: allocations
- 3. Where hidden allocations appear
- 1. Collections
- 2. Streams
- 3. String operations
- 4. Exceptions
- ⚙️ 4. Where Zero Allocation is Really Important
- 🚀 5. How Zero Allocation is Achieved
- 🧠 6. Important point about JVM
- 💣 7. Where Zero Allocation Breaks
- 📊 8. Comparison of Approaches
- 🏁 Result
Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during 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 the heap
- GC must then clean it up
- pressure arises on memory and CPU
Zero allocation aims 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) == ',') {
// processing without creating objects
start = i + 1;
}
}
3. Where hidden allocations appear
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 String objects 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
- use of primitive types (int, long)
- avoiding boxing (Integer, Long)
- manual work with arrays
- 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 |
💣 many | 🔴 high | 🟡 average | ❌ no |
| Stream API | ⚠️ average | 🟡 average | 🟡 average | ❌ almost never |
| String concat | ⚠️ average | 🟡 average | 🟡 average | ❌ no |
🏁 Result
Zero Allocation is not about "forbidding objects at all".
It’s about controlling the hot path and minimizing GC pressure.
Main rule:
The fewer objects → the less GC → the more stable latency
Оставить комментарий
My social media channel
Useful Articles:
Bitwise Operators in Java In the Java programming language, several bitwise operators are defined. These operators are applied to integer data types, such as byte, short, int, long, and char. List of ...
This article is a comprehensive guide to the key aspects of memory and runtime work in Go and Java. We will discuss fundamental concepts: execution scheduler, memory barriers, memory alignment, stack ...
Java was originally designed for multithreading and parallel computing. Over time, various methods for working with the results of asynchronous tasks have emerged—from the classic Future to modern Str...
New Articles:
Concurrency is not about “starting many threads”. It’s about agreements between them. Imagine a restaurant kitchen: — cooks (threads / goroutines) — orders (tasks) — and the main question: how do th...
Imagine a typical production service. 32 CPU hundreds of threads configuration / session / rate limits cache tens of thousands of operations per second And somewhere inside — a regular Map. At first...
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during runtime. The main idea: fewer objects → less GC → higher stability and performance. ...