Go Reference ยท Concurrency and Context
Go sync.Pool type
Reference for Go sync.Pool: Get, Put and New, reusing buffers to cut garbage collection work, why pooled objects can vanish and how to avoid stale data.
A sync.Pool is a set of temporary objects that can be saved and reused, so that code which allocates the same kind of object very often (a bytes.Buffer, a scratch slice, an encoder) does not create work for the garbage collector each time. You take an object with Get, use it, reset it and give it back with Put.
A pool is a cache, not a store. The runtime may drop any pooled object at any time, in particular during garbage collection, so you can never rely on getting back what you put in. If the pool is empty, Get calls the pool's New function to make a fresh object.
var bufPool = sync.Pool{New: func() any { return new(bytes.Buffer) }} buf := bufPool.Get().(*bytes.Buffer)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
type Pool struct { New func() any // contains filtered or unexported fields } func (p *Pool) Get() any func (p *Pool) Put(x any)
| Name | Description |
|---|---|
New |
Optional function that creates a value when the pool is empty. Set it before the first Get. |
Get |
Removes and returns an arbitrary pooled item, or calls New, or returns nil if New is nil. |
Put(x) |
Adds x to the pool for later reuse. |
Returns: Get returns any, so you assert the type: p.Get().(*bytes.Buffer).
Usage notes
- Reset before use (or before Put). A pooled buffer still holds its previous bytes. Forgetting
Resetis the classic bug that leaks data from one request into another. - Never keep using an object after
Put. Another goroutine may receive it immediately. Copy the result out first, asbuf.String()does. - Do not pool huge objects. One rare giant buffer would sit in the pool and waste memory; drop buffers above a size limit instead of putting them back (
fmtdoes this internally). - The pool is cleared by the garbage collector. Items survive roughly one GC cycle in a "victim" cache and are then discarded. A pool therefore helps under steady load and does nothing for a program that is idle.
- Safe for concurrent use. Internally each processor has its own local pool, so contention is low.
- Store pointers. Putting a non-pointer value (such as a slice header) into an
anyallocates, which defeats the purpose; use*[]byteor a pointer to a struct. - Use it only after measuring. A pool makes code harder to reason about. Run a benchmark with allocation reporting (
-benchmem) to prove it helps. - Not a resource pool. Because items can vanish, a
Poolis wrong for database connections or anything that must be closed; those need a bounded pool built from a channel.