Let's be kind and assume that prior to removal, the map has just trebled in size and the extra space wasn't used. Doesn't this imply that a map has an overhead of about 100 bytes per key/value pair? How can this be so?
There's some some waste involved in not pre-allocating the map to begin with. Check the output of this variation ( https://go.dev/play/p/vQwg3GajzXx -- n shrunk to 1,000 to stay within the playground's memory constraints) which fills the map again after the GC. You'll see the map doesn't grow back to the max size. And if you specify the size of the map up front in the `make()` function, it never grows or shrinks (or…
Maps and Memory Leaks in Go
61–70 of 72 posts
Re: Maps and Memory Leaks in Go
#62Yes, Go maps never shrink. This is good for most use cases in practice. Because in practice, map entry deletions happen seldom. And when map entry deletions are needed, users often hope maps don't shrink, to avoid potential later unnecessary memory allocations and entry moves. For example, I only do map entry deletions in one of my projects, In the project, I clear all entries of a map and re-use the map to avoid mak…
That's easily resolved with an ensureCapacity(size) method, or the other way around with shrinkToFit(size). maps being magic without a real interface means exposing new API surface is difficult though.
Re: Maps and Memory Leaks in Go
#63Earlier quoted context omitted.
Go doesn't really allow you to create a hashmap with the same generic possibilities as the built in one.
Is that still true with the support Go added for generics?
https://github.com/golang/go/blob/master/src/runtime/map.go
You also won't have `for k, v := range` iteration, but that is also likely being addressed within the next few versions.
But - no, there's not really any major ergonomic issues to basic lookups given generics these days.
Re: Maps and Memory Leaks in Go
#64Re: Maps and Memory Leaks in Go
#65Earlier quoted context omitted.
Go is just memory safe until you have a race, or so I have heard.
Every GC language by definition are memory safe, memory safety in programming does not mean than accessing the same resources from two thread should be safe.
Re: Maps and Memory Leaks in Go
#66what happened to a memory leak being some memory that was allocated but had no reference to it so couldn't be freed? If you can copy the map and release it and the memory usage drops, there is no leak?
That's the most used example of a memory leak but it is not the definition of a memory leak. If you put data into a hash map and forget the key, you leaked.
Re: Maps and Memory Leaks in Go
#67Earlier quoted context omitted.
The point is that you can remove the entries from the map, and the map won't ever shrink. If you're using large value type in the map[1], the map's dead storage will be large - by a functionally unbound amount. Most sane collection libraries shrink their backing store after some sufficiently large portion becomes dead. [1] I would argue a general purpose hash table/map should really switch to using a hash code=>index…
> Most sane collection libraries shrink their backing store after some sufficiently large portion becomes dead. If you exclude Java, and C++ and C# I think, stdlibs from being sane, sure.
I will say though that I don't consider C++'s various maps to be sane :D
Re: Maps and Memory Leaks in Go
#68FWIW i am pretty sure Java's HashMap has the same behaviour - it grows the table, but never shrinks it. Even if you call .clear(), it just clears out the table, rather than throwing the table away. I imagine there are lots of scenarios in which this is what you want, because after emptying the map, you're going to re-fill it, and it saves reallocating the table. But it would be frustrating in a scenario when that isn…
E.g.,
var map = new HashMap();
// Many things added to map
// Many but not all things removed from map
// to shrink it to size of remaining items
map = new HashMap(map);
Yeah, it's not nice, but the people who wrote the JDK are far smarter than me, so I figure they optimised for the 99% use-case, not the 1%.Re: Maps and Memory Leaks in Go
#69what happened to a memory leak being some memory that was allocated but had no reference to it so couldn't be freed? If you can copy the map and release it and the memory usage drops, there is no leak?
Yeah, that makes the title pretty much clickbait, because a memory leak in a memory-safe language would really be a big deal...
Re: Maps and Memory Leaks in Go
#70FWIW i am pretty sure Java's HashMap has the same behaviour - it grows the table, but never shrinks it. Even if you call .clear(), it just clears out the table, rather than throwing the table away. I imagine there are lots of scenarios in which this is what you want, because after emptying the map, you're going to re-fill it, and it saves reallocating the table. But it would be frustrating in a scenario when that isn…
You're right that Java's Hashmap only ever resizes upwards - the most common use case. However, if there's a need to remove that memory after clearing() then the typical use case is to do something that allows the GC to sort it. E.g., var map = new HashMap (); // Many things added to map // Many but not all things removed from map // to shrink it to size of remaining items map = new HashMap (map); Yeah, it's not nice…
m := make(map[Bla]Bla)
// add to map
// mark map as ready for GC
m = make(map[Bla]Bla)