2021年3月3日星期三

Does repeated storage and key deletion of golang map lead to memory leakage?

I know that deleting the key of the map will not free the memory.

Will the following operations cause the memory occupied by map to increase continuously?

1.Define a global variable map

2.Store and delete the same key repeatedly

package main    import (      "log"      "runtime"  )    var m = make(map[int]bool)    func main() {      for i := 0; i < 50000; i++ {          m[i] = true          delete(m, i)            readMemStats()      }  }    func readMemStats() {      var ms runtime.MemStats      runtime.ReadMemStats(&ms)      log.Printf("Alloc = %v kb TotalAlloc = %v kb Sys = %v kb NumGC = %v\n", ms.Alloc/1024, ms.TotalAlloc/1024, ms.Sys/1024, ms.NumGC)  }    
https://stackoverflow.com/questions/66468012/does-repeated-storage-and-key-deletion-of-golang-map-lead-to-memory-leakage March 04, 2021 at 11:09AM

没有评论:

发表评论