Reducing Allocations: sync.Pool and Arenas
Object reuse, sync.Pool, and reducing GC pressure
Reducing Allocations: sync.Pool and Arenas is a free Go Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
Why reduce allocations?
Each heap allocation triggers eventual GC work. Reducing allocations lowers GC frequency and pause durations, improving throughput and tail latency in high-traffic services.
Profiling first
Always profile before optimising. Use go test -benchmem and -alloc_space heap profiles to find the actual hot allocation paths. Do not optimise based on intuition.
sync.Pool
sync.Pool is a thread-safe pool of reusable objects. Use it for short-lived objects that are frequently allocated and discarded (byte buffers, scratch maps, request contexts).
var pool = sync.Pool{
New: func() any { return &bytes.Buffer{} },
}
func process(data []byte) {
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
buf.Write(data)
// use buf...
pool.Put(buf)
}Pool is not a cache
The GC may clear pool items at any time. Pool is for reducing allocation pressure, not for caching long-lived data. Retrieved objects may be from any goroutine.
Always Reset before reuse
Clear pooled objects before use — they may contain data from a previous caller. For bytes.Buffer, call Reset(); for slices, reslice to [:0].
Pre-allocating slices
Pre-allocate slices with make([]T, 0, n) when the final length is known. This avoids repeated doubling and copying as the slice grows.
results := make([]Result, 0, len(input)) // no reallocationsString builder
Use strings.Builder instead of string concatenation to avoid intermediate string allocations in loops:
var sb strings.Builder
for _, s := range parts { sb.WriteString(s) }
result := sb.String()bytes.Buffer pooling
bytes.Buffer is one of the most commonly pooled types. Pool it to avoid repeated allocation in JSON encoding, HTTP response building, and template rendering.
Go arenas (experimental)
Go 1.20+ includes an experimental arena package (golang.org/x/exp/arena). Arenas allocate many objects in a single large block that is freed at once, bypassing per-object GC.
Value types
Small structs passed by value avoid heap allocation entirely. Avoid pointer-to-small-struct patterns in hot code; returning values is often cheaper.
Measuring impact
Run benchmarks with -benchmem before and after and compare with benchstat. Aim to reduce allocs/op for the hot path, not just ns/op.
BenchmarkProcess-8 1000000 125 ns/op 64 B/op 2 allocs/op
// After pooling:
BenchmarkProcess-8 1000000 48 ns/op 0 B/op 0 allocs/opQuick Check
What is the key characteristic that makes sync.Pool safe for reducing allocations?
Recap: Reducing Allocations
Key points:
- Profile first: -benchmem and heap -alloc_space before optimising
- sync.Pool for frequently allocated/discarded objects; always Reset before reuse
- Pre-allocate slices with make([]T, 0, n); strings.Builder for concatenation
- Value types avoid heap allocation; benchmark to verify improvements
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Reducing Allocations: sync.Pool and Arenas” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Reducing Allocations: sync.Pool and Arenas”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
What will I learn in “Reducing Allocations: sync.Pool and Arenas”?
Object reuse, sync.Pool, and reducing GC pressure You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Reducing Allocations: sync.Pool and Arenas” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Stack vs Heap and Escape Analysis
- The Go Memory Model and Happens-Before
- Garbage Collector Internals
- Reducing Allocations: sync.Pool and Arenas