← Back to the shelf
Appearance

Dispatches / a short note

algorithm

Understanding golang's memory allocator

Inner workings of golang memory allocator

If Go made a system call every time you wrote make([]byte, 100) or &MyStruct{}, performance would be terrible…
… So the Go runtime takes a different approach: it asks the OS for large chunks of memory upfront

I looked a bit about this, the same is true for C too. The malloc() does the same by reserving a big chunk of memory in prior (arenas). malloc() doesn’t cost extra system call whenever it is invoked (mostly, just like go).

The “three-level hierarchy” is simply ingenious. Having a localized preallocated (mcache, lockfree) reserve per CPU core to serve small frequent allocations. Upon exhaustion, mcache requests for a new reserve from the shared mcentral cache (with minor brief locks). Also while sending relatively rare infrequent big memory requests to a shared (mheap) reserve directly (unavoidable usual locks involved) - a global page allocator… Statistically, this reduces the number of system calls required by a LOT.

If mheap runs out, it has to ask OS to reserve new arena (64MiB).

Alright, time to sweep now. I have not elaborated on how span and nospan objected are tracked in allocBits bitmap, pls refer the article if you want to know more. So yea, walk the bit map, and identify the freed up spans among the used and reallocate it back to mheap. OS doesn’t reclaim it yet.

(Understanding the Go Runtime) The Memory Allocator: https://internals-for-interns.com/posts/go-memory-allocator/

Actually wow, all these years I had a pretty bad opinion about garbage collectors, but after reading the article - it feels like they were put in place as an “optimization” effort. To min-max available resources… So, then what makes GC programs run comparitively slower to systems programming? Is it simply because of the bad code? Or, was it because of the nature of the function?

Low level programs have to be light in general - limited compute, limited memory. And they have to run in a hyper low latency mode. Whereas, the actual applications built over it are data intensive - completely different environment (where min-maxing pays off more?). I don’t have a definite answer jet. Johnathan Blow once talked about Garbage Collector, might have to watch that.

Discussion link: https://news.ycombinator.com/item?id=47129801