Maps and Memory Leaks in Go

  1. Allocate an empty map.
  2. Add 1 million elements.
  3. Remove all the elements, and run a Garbage Collection (GC).
0 MB   <-- After m is allocated
461 MB <-- After we add 1 million elements
293 MB <-- After we remove 1 million elements
Figure 1 — A hash table example with a focus on bucket 0.
Figure 2 — In case of a bucket overflow, Go allocates a new bucket and links the previous bucket to it.


❤️ Enjoying my work? You can consider becoming a GitHub sponsor:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Teiva Harsanyi

Teiva Harsanyi


Software Engineer @Docker 🐳 | 📖 100 Go Mistakes author | 改善