0Pricing
Go Academy · Lesson

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 — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 reallocations

String 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/op

Quick 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

Frequently asked questions

Is the “Reducing Allocations: sync.Pool and Arenas” lesson free?

Yes — the full text of “Reducing Allocations: sync.Pool and Arenas” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Stack vs Heap and Escape Analysis
  2. The Go Memory Model and Happens-Before
  3. Garbage Collector Internals
  4. Reducing Allocations: sync.Pool and Arenas
← Back to Go Academy