Ever wondered how does Go manage memory allocation? In this talk we are going to explore Go’s memory allocator and understand how its algorithm interacts with the operating system to manage memory!
types of allocations ◦ Small allocations (<= 32 kB) ◦ Large allocations • Manages memory in units called Spans ◦ Runs of contiguous memory pages ◦ Metadata is kept separated from the allocation arena 25
Tightly coupled with the allocator ◦ Makes hard (impossible?) to replace with other implementations • Three types of allocations ◦ Tiny Allocations (size < 16 bytes, no pointers) ◦ Small Allocations (size <= 32 kbytes) ◦ Large Allocations 50
pointers and size < 16 bytes The main targets of tiny allocator are small strings and standalone escaping variables. On a json benchmark the allocator reduces number of allocations by ~12% and reduces heap size by ~20%. 66
spans that were swept more than 5 minutes ago • In Linux, uses the madvise(2) syscall 69 Go’s Allocator - Releasing memory to the OS madvise(addr, size, _MADV_DONTNEED)