Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 22, 2023 09:42
Show Gist options
  • Save yumed15/07932c9e4e61b84602f6d927e00fd428 to your computer and use it in GitHub Desktop.
Save yumed15/07932c9e4e61b84602f6d927e00fd428 to your computer and use it in GitHub Desktop.
// The maximum size of explicitly
// declared variables on stacks is 10MB
type T struct {
X [10 * 1000 * 1000]byte // 10MB
}
var global interface{}
func BenchmarkAllocOnHeap(b *testing.B) {
b.ReportAllocs()
for i := 0; i <= b.N; i++ {
global = &T{}
}
}
func BenchmarkAllocOnStack(b *testing.B) {
b.ReportAllocs()
for i := 0; i <= b.N; i++ {
local := T{}
_ = local
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment