Created
February 21, 2011 16:46
-
-
Save rogpeppe/837326 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 ( | |
"log" | |
"runtime" | |
) | |
func main() { | |
m := mkmap() | |
for _ = range m { | |
break | |
} | |
log.Print("Garbage collecting...") | |
m = nil | |
runtime.GC() | |
log.Println("allocated", runtime.MemStats.HeapAlloc) | |
m = mkmap() | |
log.Print("Re-adding entries...") | |
for i := 0; i < 1e6; i++ { | |
m[uint64(i)] = nil | |
} | |
log.Print("Done...") | |
log.Println("final allocated", runtime.MemStats.HeapAlloc) | |
} | |
func mkmap() map[uint64]interface{} { | |
m := make(map[uint64]interface{}) | |
log.Print("Making big map...") | |
for i := 0; i < 1e6; i++ { | |
m[uint64(i)] = nil | |
} | |
log.Print("Done...") | |
log.Print("Deleting entries...") | |
for i := range m { | |
m[i] = nil, false | |
} | |
log.Println("allocated", runtime.MemStats.HeapAlloc) | |
return m | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment