How the JVM allocates objects
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
explain why allocation is cheap and what it costs later.
Planned topics
- Most objects are allocated in the young generation by bumping a pointer inside a thread-local allocation buffer (TLAB); no lock needed.
- Cheap allocation is why short-lived objects are fine in Java.
- Allocation rate (MB/s) drives how often the GC runs.
- Very large objects may go straight to a special region or the old generation (collector-specific).
OutOfMemoryError: Java heap spacemeans the live set no longer fits, not that allocation is slow.
Draft outline details
Demonstration
List<OrderLine> lines = new ArrayList<>(); // 1 object, then N OrderLine objects
for (var item : cart) lines.add(new OrderLine(item));Check your understanding
What do you measure to know GC pressure?
Show answer
Allocation rate, with GC frequency and pause times.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.