Skip to content
Browse files
runtime: limit the number of map overflow buckets
Consider repeatedly adding many items to a map
and then deleting them all, as in #16070. The map
itself doesn't need to grow above the high water
mark of number of items. However, due to random
collisions, the map can accumulate overflow

Prior to this CL, those overflow buckets were
never removed, which led to a slow memory leak.

The problem with removing overflow buckets is
iterators. The obvious approach is to repack
keys and values and eliminate unused overflow
buckets. However, keys, values, and overflow
buckets cannot be manipulated without disrupting

This CL takes a different approach, which is to
reuse the existing map growth mechanism,
which is well established, well tested, and
safe in the presence of iterators.
When a map has accumulated enough overflow buckets
we trigger map growth, but grow into a map of the
same size as before. The old overflow buckets will
be left behind for garbage collection.

For the code in #16070, instead of climbing
(very slowly) forever, memory usage now cycles
between 264mb and 483mb every 15 minutes or so.

To avoid increasing the size of maps,
the overflow bucket counter is only 16 bits.
For large maps, the counter is incremented

Fixes #16070

Change-Id: If551d77613ec6836907efca58bda3deee304297e
Run-TryBot: Josh Bleecher Snyder <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Keith Randall <>
  • Loading branch information
josharian committed Sep 13, 2016
1 parent 0cd3ecb commit 9980b70
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 78 deletions.
@@ -182,20 +182,22 @@ func hmap(t *Type) *Type {

bucket := mapbucket(t)
var field [8]*Field
field[0] = makefield("count", Types[TINT])
field[1] = makefield("flags", Types[TUINT8])
field[2] = makefield("B", Types[TUINT8])
field[3] = makefield("hash0", Types[TUINT32])
field[4] = makefield("buckets", Ptrto(bucket))
field[5] = makefield("oldbuckets", Ptrto(bucket))
field[6] = makefield("nevacuate", Types[TUINTPTR])
field[7] = makefield("overflow", Types[TUNSAFEPTR])
fields := []*Field{
makefield("count", Types[TINT]),
makefield("flags", Types[TUINT8]),
makefield("B", Types[TUINT8]),
makefield("noverflow", Types[TUINT16]),
makefield("hash0", Types[TUINT32]),
makefield("buckets", Ptrto(bucket)),
makefield("oldbuckets", Ptrto(bucket)),
makefield("nevacuate", Types[TUINTPTR]),
makefield("overflow", Types[TUNSAFEPTR]),

h := typ(TSTRUCT)
h.Noalg = true
h.Local = t.Local
t.MapType().Hmap = h
h.StructType().Map = t

0 comments on commit 9980b70

Please sign in to comment.