Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A teammate proposes object pooling for small, short-lived request DTOs to reduce GC. How do you respond?

I would push back and ask for measurements first. In a modern JVM, allocating a small object is a pointer bump in a thread-local buffer, and young-generation collection costs time for live objects, not dead ones, so short-lived DTOs are almost free. A pool adds synchronization, the risk of handing out objects with stale data, and keeps objects alive long enough to be promoted to the old generation, which is where GC gets expensive. Pooling still makes sense for expensive resources such as connections, threads and large buffers.

Background to this question

Object pooling was good advice for Java in the late 1990s, when allocation and garbage collection were slow, and the idea survives in many teams. This question tests whether we can respond to a well-meant performance proposal with reasoning and data instead of opinion, and whether we understand how today's JVM allocates and collects. In this answer, we will recap how allocation and young-generation collection work, see what a pool would cost, look at the cases where pooling is right, and finish with how we would respond to the teammate, including what to measure.

1A Quick Recap: Why Small Allocations Are Cheap

Each thread in the JVM has its own Thread-Local Allocation Buffer, or TLAB, a chunk of the young generation reserved for it. Allocating a small object means checking that it fits and moving the buffer's top pointer forward, a few instructions with no locks. Zeroing the fields and writing the header add a little more. For a DTO with five fields, this is in the order of nanoseconds.

The young generation is collected by copying. The collector finds the objects that are still live, copies them out, and reuses the whole region. Dead objects are never visited; they are simply overwritten next time. So a request DTO that is created, serialized to JSON and dropped within one request costs the garbage collector almost nothing. This is the generational hypothesis: most objects die young, and the JVM is built to make that case cheap.

2What a Pool Would Cost

A pool turns short-lived objects into long-lived ones, and every advantage above reverses. Here is what a simple pool for a DTO looks like and what can go wrong.

Java
final class OrderDtoPool {
private final ConcurrentLinkedQueue<OrderDto> free = new ConcurrentLinkedQueue<>();
OrderDto acquire() {
OrderDto d = free.poll(); // shared queue: contention under load
return d != null ? d : new OrderDto();
}
void release(OrderDto d) {
d.reset(); // forget one field here and the next request sees old data
free.offer(d);
}
}
// Request 1: acquire, fill customerEmail, release (reset() forgot customerEmail)
// Request 2: acquire, fill other fields, serialize -> response contains request 1's customer email
Fresh DTOs versus pooled DTOs
Concernnew OrderDto() per requestPooled OrderDto
AllocationTLAB pointer bump, no locksshared queue, contention under load
GC costdead objects are free in young GCpromoted to old gen, tracked by barriers
Correctnessalways starts cleanstale fields if reset() misses one
Use after releaseimpossiblesilent data corruption
Codeplain constructor or recordacquire, try, finally release

The first cost is synchronization. Every acquire and release touches a shared structure, which under load is much slower than a TLAB bump. The second is correctness. If reset() misses a field, or code keeps using a DTO after releasing it, one user's data appears in another user's response, which is a security incident, not just a bug. The third is GC. Pooled objects survive many collections, so they are promoted to the old generation. Once there, every time we store a reference to a young object in them, the collector must track it through its write barrier and card table, and old objects are only reclaimed by the more expensive old-generation or mixed collections.

3When Pooling Is the Right Answer

Pooling is still right when creating the object is expensive for reasons that have nothing to do with memory allocation. Database connections are the classic case: opening one means a network handshake, authentication and session setup, often several milliseconds, which is why we use a connection pool such as HikariCP. Platform threads are expensive to create and hold a large stack, so we use thread pools for them, while virtual threads are cheap and should not be pooled. Large buffers, especially direct ByteBuffers used for I/O, are costly to allocate and to free, and frameworks like Netty pool them for that reason.

The common thread is that the cost being avoided is external: an operating system resource, a network round trip, or a large block of memory. A small DTO has none of these.

4How We Would Respond

We would thank the teammate for thinking about performance and then ask for the data that shows a problem. For allocation, a JFR recording with allocation profiling, or async-profiler in allocation mode, shows which code allocates the most bytes. For GC, the logs from -Xlog:gc* show how often young collections run, how long they pause, and how much is promoted. If young pauses are a few milliseconds and promotion is low, there is no GC problem for a pool to solve.

Text
# a recording of the service under realistic load
java -XX:StartFlightRecording=duration=120s,settings=profile,filename=load.jfr -jar app.jar
# GC log with pause times and promotion
java -Xlog:gc*:file=gc.log:time,uptime -jar app.jar
Step throughResponding to a pooling proposal
  1. Is there measured GC or allocation trouble?JFR allocation profile, GC logs
    YesFind the top allocation sitesNoKeep plain objects
  2. Is the object expensive to create?connection, thread, large buffer
    YesUse an existing pool libraryNoSimplify the data flow first
  3. Still a problem after simplifying?
    YesTune the young generation or collectorNoDone, no pool needed

3 steps in order. Play them, or step through them one at a time.

If the data does show allocation pressure, we look at what allocates, and it is rarely the DTO itself. More often it is intermediate copies: building a large String with repeated concatenation, mapping an entity to one DTO and then to another, reading a whole response body into a byte array before parsing it, or boxing numbers in collections. Simplifying the data flow, streaming instead of buffering, and removing the extra mapping steps usually cuts allocation far more than pooling the final object, and without any of its risks. Tuning the young generation size or the collector is the next step if pauses still matter.

5Follow-up questions

Would records make the DTOs cheaper?

Records are not cheaper to allocate than ordinary classes, but they are immutable, which makes pooling impossible by design and sharing safe, and their compact syntax makes small DTOs pleasant to use. The point is that creating a fresh record per request is the normal, fast path.

Can a pool ever reduce GC pause times?

In some very latency-sensitive systems, such as trading platforms, teams pre-allocate and reuse objects to avoid any allocation on the hot path, accepting the complexity. That is a deliberate architecture with careful measurement, not a general web service practice, and modern low-pause collectors such as ZGC have made it unnecessary for most systems.

What about pooling to reduce memory usage?

A pool usually increases memory use, because it keeps idle objects alive even when the load drops. Fresh objects only use memory while they are needed. If memory is tight, reducing the size of what we allocate, for example not loading full entities when a few columns are needed, is the effective fix.

6Conclusion

In this answer, we saw that small, short-lived DTOs are nearly free in a modern JVM, because allocation is a TLAB pointer bump and young collections only pay for live objects. A pool would add contention, risk leaking stale data between requests and promote objects to the old generation, making GC more expensive, while pooling remains right for connections, platform threads and large buffers. The response is to ask for allocation profiles and GC logs, and to reduce allocation by simplifying the data flow before considering any pool.

What to say in the interviewAbout 90 seconds aloud

Open with

I would thank them for looking at performance, but I would not pool small DTOs without evidence. In a modern JVM, allocating a small object is a pointer bump in the thread's TLAB, a few instructions without locks, and young-generation collection is a copying collection, so its cost depends on live objects, not dead ones.

Then, if they want more

A DTO that lives for one request is close to free. A pool reverses that. Acquire and release go through a shared structure, which adds contention. If reset misses a field, or code uses a DTO after releasing it, one user's data can leak into another user's response, which is a security bug.

And pooled objects live long, get promoted to the old generation, and then cost write-barrier and card-table work plus more expensive old collections. Pools do make sense when creation is expensive for external reasons: database connections, platform threads, large or direct buffers, and we already use libraries for those.

So I would ask for data: a JFR or async-profiler allocation profile under realistic load, and GC logs showing pause times and promotion. If there is allocation pressure, it usually comes from intermediate copies, extra mapping layers or buffering whole payloads, and simplifying that flow, or tuning the young generation, helps more than a pool, without the risks.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A teammate proposes object pooling for small, short-lived request DTOs to reduce GC. How...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/7edeac5a53/