Senior · Memory and runtime
Retention, pools and ownership
Can a tiny slice retain a very large allocation?
Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.
token := append([]byte(nil), body[:20]...)Reveal a strong answer
A slice keeps its backing array reachable. Keeping a short subslice of a large buffer can retain the full allocation; retaining pointer elements may also retain their referenced objects.
Copy only the needed bytes into independent storage when lifetime separation matters. Clearing obsolete pointers helps release referenced objects, but it does not shrink the backing allocation itself.
sync.Pool is a temporary allocation reuse mechanism whose contents may disappear at any time. Do not use it as a durable cache, a resource limit, or a place to return buffers still referenced by a response.
Common trap
Returning a buffer to a pool while another goroutine still reads it creates corruption or a data race.
Try it
A parser stores the first 20 bytes of each 20 MB request. How would you test and fix retention?
Worked solution
Copy the retained token into a small independent allocation, release the request and compare live heap after collection in a controlled test. Keep the measurement separate from a throughput benchmark.
Change the constraint
How would oversized pooled buffers change memory use after a rare large request?
How did you do?
Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.
Not marked. Progress stays on this browser, separate from course completion.
Primary references
Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.