Just to be clear, the memory used by the values *are *freed. In your example, those are the Person structs. It is only the internal memory used by the map that is not freed. See https://play.golang.org/p/fWOIbvjFjyB. In that test, the "internal" memory that is not freed is about 14 bytes per entry.
Of course, keep in mind that nothing is freed until a GC is done. On Monday, August 27, 2018 at 5:00:14 AM UTC-4, Kasun Vithanage wrote: > > I've a map which has set of keys and pointing to some structs like this. > In here i allocate lot of entries and trying to delete them. But the memory > usage is not shrinking. > > According to this issue <https://github.com/golang/go/issues/20135> it > seems how go behave at this point. In there its suggested to create a new > map and move all data there for reduced memory usage. But that seems not a > better option as it > is an expensive operation against such large map. > > What is the best way to delete a key from map freeing the memory occupied > by the Value(a pointer in this case). > > type Person struct { > Name string > } > > func NewPerson(name string) *Person { > return &Person{Name: name} > } > > func main() { > m := make(map[int]*Person) > > for i := 0; i < 1000000000; i++ { > m[i] = NewPerson("Person" + strconv.Itoa(i)) > } > > for index := 0; index < 10000; index++ { > m[index] = nil > delete(m, index) > } > } > > > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.