Skip to content

Instantly share code, notes, and snippets.

@calmh
Created November 30, 2015 13:57
Show Gist options
  • Save calmh/f7bfe01f299c629b10cf to your computer and use it in GitHub Desktop.
Save calmh/f7bfe01f299c629b10cf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
// One million slices
allocs := make([][]byte, 1e6)
// ... of 1 KiB each
for i := 0; i < 1e6; i++ {
allocs[i] = make([]byte, 1024)
allocs[i][42] = 42 // Make sure the page is in use and allocated to us
}
fmt.Println("Allocations done, sleeping.")
for i := 0; i < 1e6; i++ {
if allocs[i][42] != 42 {
panic("wat")
}
time.Sleep(time.Second)
runtime.GC()
}
}
// $ GODEBUG=gctrace=1 go run gctest1.go
// ...
// gc 42 @36.317s 5%: 1.3+0+0+0+45 ms clock, 11+0+0+0.049/65/0.15+361 ms cpu, 999->999->999 MB, 999 MB goal, 8 P (forced)
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
allocs := make([][]byte, 1e6)
backing := make([]byte, 1e6*1024)
for i := 0; i < 1e6; i++ {
allocs[i] = backing[i*1024 : (i+1)*1024]
allocs[i][42] = 42 // Make sure the page is in use and allocated to us
}
fmt.Println("Allocations done, sleeping.")
for i := 0; i < 1e6; i++ {
if allocs[i][42] != 42 {
panic("wat")
}
time.Sleep(time.Second)
runtime.GC()
}
}
// $ GODEBUG=gctrace=1 go run gctest2.go
// gc 42 @42.525s 2%: 0.055+0+0+0+27 ms clock, 0.38+0+0+0.016/42/0.11+191 ms cpu, 999->999->999 MB, 999 MB goal, 8 P (forced)
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
backing := make([]byte, 1e6*1024)
for i := 0; i < 1e6; i++ {
backing[i*1024+42] = 42 // Make sure the page is in use and allocated to us
}
fmt.Println("Allocations done, sleeping.")
for i := 0; i < 1e6; i++ {
if backing[i*1024+42] != 42 {
panic("wat")
}
time.Sleep(time.Second)
runtime.GC()
}
}
// $ GODEBUG=gctrace=1 go run gctest3.go
// gc 42 @41.423s 0%: 0.050+0+0+0+0.34 ms clock, 0.35+0+0+0/0.065/0.041+2.4 ms cpu, 976->976->976 MB, 976 MB goal, 8 P (forced)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment