What is `sync.Pool` and how does it optimize memory?
Updated Apr 28, 2026
Short answer
sync.Pool is a thread-safe cache for reusing temporary objects, reducing memory allocations and Garbage Collection pressure.
Deep explanation
When an application frequently allocates short-lived objects (like byte buffers for network I/O), the GC has to work hard to sweep them. sync.Pool caches these allocated objects. You Get() an object, use it, and Put() it back. If the pool is empty, it calls an allocation function. Note: The GC periodically clears sync.Pool, so it shouldn't hold long-lived state.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro