Problem Statement
Your JSON API service allocates and discards thousands of byte buffers per second, causing GC pauses. Implement object pooling using sync.Pool to reuse allocations.
What is sync.Pool?
A pool of temporary objects that can be saved and retrieved. Objects may be removed by GC at any time without notification.
Basic Usage
Benchmark: With vs Without Pool
JSON Encoder Pool (Real-World)
Pool Lifecycle with GC
Common Mistakes
Follow-up Questions
- Why doesn't sync.Pool have a Size() method?
- When would sync.Pool hurt performance?
- How does sync.Pool interact with GOMAXPROCS?