Skip to content

Instantly share code, notes, and snippets.

@teivah
Last active September 27, 2022 12:11
Show Gist options
  • Save teivah/9befd7b173a3148da59c581c58491e29 to your computer and use it in GitHub Desktop.
Save teivah/9befd7b173a3148da59c581c58491e29 to your computer and use it in GitHub Desktop.
func main() {
n := 1_000_000
m := make(map[int][128]byte)
printAlloc()
for i := 0; i < n; i++ { // Adds 1 million elements
m[i] = [128]byte{}
}
printAlloc()
for i := 0; i < n; i++ { // Deletes 1 million elements
delete(m, i)
}
runtime.GC() // Triggers a manual GC
printAlloc()
runtime.KeepAlive(m) // Keeps a reference to m so that the map isn’t collected
}
func printAlloc() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("%d KB\n", m.Alloc/1024)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment