Skip to content

Instantly share code, notes, and snippets.

@teivah
Last active March 14, 2024 12:27
Show Gist options
  • Save teivah/cb49f8a5150d8513d6cca2968a18a162 to your computer and use it in GitHub Desktop.
Save teivah/cb49f8a5150d8513d6cca2968a18a162 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"runtime"
)
func main() {
// Init
n := 1_000_000
m := make(map[int][128]byte)
runtime.GC()
printAlloc()
// Add elements
for i := 0; i < n; i++ {
m[i] = randBytes()
}
runtime.GC()
printAlloc()
// Remove elements
clear(m)
// End
runtime.GC()
printAlloc()
runtime.KeepAlive(m)
}
func randBytes() [128]byte {
return [128]byte{}
}
func printAlloc() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("%d MB\n", m.Alloc/1024/1024)
}
@teivah
Copy link
Author

teivah commented Mar 14, 2024

Output:

0 MB
293 MB
293 MB <-- Same memory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment