在Go中,map中存放map,上層map執行delete,子層map佔用的內存會釋放,無需手動先釋放子map內存,再在上層map執行刪除。json
在C++中,若是使用了map包含map的數據結構,當要釋放上層map的某一項時,須要手動釋放對應的子map佔用的內存,而在Go中,垃圾回收讓內存管理變得如此簡單。數據結構
package main import ( "log" "runtime" ) var lastTotalFreed uint64 var intMap map[int]int var cnt = 8192 func main() { printMemStats() initMap() runtime.GC() printMemStats() log.Println(len(intMap)) for i := 0; i < cnt; i++ { delete(intMap, i) } log.Println(len(intMap)) runtime.GC() printMemStats() intMap = nil runtime.GC() printMemStats() } func initMap() { intMap = make(map[int]int, cnt) for i := 0; i < cnt; i++ { intMap[i] = i } } func printMemStats() { var m runtime.MemStats runtime.ReadMemStats(&m) log.Printf("Alloc = %v TotalAlloc = %v Just Freed = %v Sys = %v NumGC = %v\n", m.Alloc/1024, m.TotalAlloc/1024, ((m.TotalAlloc-m.Alloc)-lastTotalFreed)/1024, m.Sys/1024, m.NumGC) lastTotalFreed = m.TotalAlloc - m.Alloc }
結果ui
2018/09/29 20:09:25 Alloc = 65 TotalAlloc = 65 Just Freed = 0 Sys = 1700 NumGC = 0 2018/09/29 20:09:25 Alloc = 387 TotalAlloc = 391 Just Freed = 3 Sys = 3076 NumGC = 1 2018/09/29 20:09:25 8192 2018/09/29 20:09:25 0 2018/09/29 20:09:25 Alloc = 387 TotalAlloc = 392 Just Freed = 1 Sys = 3140 NumGC = 2 2018/09/29 20:09:25 Alloc = 74 TotalAlloc = 394 Just Freed = 314 Sys = 3140 NumGC = 3
package main import ( "log" "runtime" ) var intMapMap map[int]map[int]int var cnt = 1024 var lastTotalFreed uint64 // size of last memory has been freed func main() { // 1 printMemStats() // 2 initMapMap() runtime.GC() printMemStats() // 3 fillMapMap() runtime.GC() printMemStats() // 4 log.Println(len(intMapMap)) for i := 0; i < cnt; i++ { delete(intMapMap, i) } log.Println(len(intMapMap)) runtime.GC() printMemStats() // 5 intMapMap = nil runtime.GC() printMemStats() } func initMapMap() { intMapMap = make(map[int]map[int]int, cnt) for i := 0; i < cnt; i++ { intMapMap[i] = make(map[int]int, cnt) } } func fillMapMap() { for i := 0; i < cnt; i++ { for j := 0; j < cnt; j++ { intMapMap[i][j] = j } } } func printMemStats() { var m runtime.MemStats runtime.ReadMemStats(&m) log.Printf("Alloc = %v TotalAlloc = %v Just Freed = %v Sys = %v NumGC = %v\n", m.Alloc/1024, m.TotalAlloc/1024, ((m.TotalAlloc-m.Alloc)-lastTotalFreed)/1024, m.Sys/1024, m.NumGC) lastTotalFreed = m.TotalAlloc - m.Alloc }
結果code
2018/09/29 20:10:27 Alloc = 64 TotalAlloc = 64 Just Freed = 0 Sys = 1700 NumGC = 0 2018/09/29 20:10:27 Alloc = 41154 TotalAlloc = 41157 Just Freed = 3 Sys = 46026 NumGC = 5 2018/09/29 20:10:27 Alloc = 41241 TotalAlloc = 41293 Just Freed = 48 Sys = 47082 NumGC = 6 2018/09/29 20:10:27 1024 2018/09/29 20:10:27 0 2018/09/29 20:10:27 Alloc = 114 TotalAlloc = 41295 Just Freed = 41128 Sys = 47082 NumGC = 7 2018/09/29 20:10:27 Alloc = 74 TotalAlloc = 41296 Just Freed = 41 Sys = 47082 NumGC = 8
若是這篇文章對你有幫助,請點個贊/喜歡,讓我知道個人寫做是有價值的,感謝。