I think there is a major different: the memory allocated for key "index" in hashmap, if you assign m[index] to nil, the allocated memory for hashed key "index" still exist, when delete the allocated memory will be deleted as well. Both will have same affect on the pointer to struct, it will be collected by Garbage Collector
Vào Th 2, 27 thg 8, 2018 vào lúc 16:00 Kasun Vithanage < alanka...@gmail.com> đã viết: > 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. > -- 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.