Last active
September 27, 2022 12:11
-
-
Save teivah/9befd7b173a3148da59c581c58491e29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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