What is the proper way to delete a key which holding a pointer in a map

857 views
Skip to first unread message

Kasun Vithanage

unread,
Aug 27, 2018, 5:00:14 AM8/27/18
to golang-nuts
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 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)
       }
}



Hau Ma

unread,
Aug 27, 2018, 5:32:53 AM8/27/18
to alan...@gmail.com, golan...@googlegroups.com
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 <alan...@gmail.com> đã viết:
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kasun Vithanage

unread,
Aug 27, 2018, 5:37:53 AM8/27/18
to golang-nuts
I simply want to delete the memory allocated by Value too. When i checked memory with MemStats, its still same as when i added entries to map

Jan Mercl

unread,
Aug 27, 2018, 5:43:39 AM8/27/18
to Kasun Vithanage, golang-nuts
On Mon, Aug 27, 2018 at 11:38 AM Kasun Vithanage <alan...@gmail.com> wrote:

> I simply want to delete the memory allocated by Value too. When i checked memory with MemStats, its still same as when i added entries to map

See https://github.com/golang/go/issues/20135#issuecomment-297490415

"The only available workaround is to make a new map and copy in elements from the old."


--

-j

Kasun Vithanage

unread,
Aug 27, 2018, 5:53:48 AM8/27/18
to golang-nuts
Yeah saw that, but what about a map with around 1000000000 entries. this will add a lot of overhead i guess.

oju...@gmail.com

unread,
Aug 27, 2018, 10:53:06 AM8/27/18
to golang-nuts
Replace your 1 billion entry map with a large number of small maps.
When you need to remove an entry you just substitute a small map.

jake...@gmail.com

unread,
Aug 27, 2018, 12:26:16 PM8/27/18
to golang-nuts
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.

Kasun Vithanage

unread,
Aug 27, 2018, 3:13:00 PM8/27/18
to golang-nuts
thanks this answer will work

keith....@gmail.com

unread,
Aug 27, 2018, 7:38:19 PM8/27/18
to golang-nuts
FYI shrinking maps on delete is issue 20135.

Naveen Kak

unread,
Apr 26, 2020, 5:49:06 PM4/26/20
to golang-nuts
Is this really working.? For me also the memory keeps growing ..i have a nested map structure though..only when the top level mao is gone i see memory shrinking.
Just deleting entries from the top level map doesn't see to solve any issue, 
Reply all
Reply to author
Forward
0 new messages