Last active
March 14, 2024 12:27
-
-
Save teivah/cb49f8a5150d8513d6cca2968a18a162 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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: