// example1.go
package main
import "runtime"
func main() {
m := new(runtime.MemStats)
runtime.ReadMemStats(m)
println(m.Alloc, m.Mallocs)
cap := 1024 * 1024 * 3
s := make([]byte, cap)
_ = s
runtime.ReadMemStats(m)
println(m.Alloc, m.Mallocs)
}
// example2.go
package main
import "runtime"
func main() {
m := new(runtime.MemStats)
runtime.ReadMemStats(m)
println(m.Alloc, m.Mallocs)
cap := 1024 * 1024 * 4
s := make([]byte, cap)
_ = s
runtime.ReadMemStats(m)
println(m.Alloc, m.Mallocs)
}
Then check -gcflags=-m to see if the compiler is choosing a different escape analysis depending on the size of your allocation.