Go uses a non-generational concurrent garbage collector. Most tuning is “write less garbage” and “measure with pprof”.
The compiler escapes variables to the heap when their lifetime cannot be proven stack-safe (e.g. returned pointers to local variables, captured by closures). Not every new or &T escapes — trust go build -gcflags=-m escape analysis logs when optimizing hot paths.
make([]T, 0, n) or make(map[K]V, hint) when size is known to reduce reallocations.Efficient string assembly.
var b strings.Builder
for _, s := range parts {
b.WriteString(s)
}
return b.String()
Reuse temporary allocations under strict lifetimes — objects may be cleared at any GC; never assume pooled values persist.
go test -cpuprofile=cpu.prof
go tool pprof cpu.prof
go test -memprofile=mem.prof
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
Foo()
}
}
Inlining, bounds check elimination, and devirtualization happen automatically; keep hot loops simple and allocation-free when possible.