NOTE
3.9 Golang Heap Management
Heap, heap memory layout, allocation units, mcache/mcentral/mheap, escape analysis, allocation flow, and references.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is the Heap?
- Large space, with data stored for a relatively long time. The heap is at lower addresses and grows from low addresses toward high addresses.
- Allocation: manually allocated by the programmer.
- Reclamation: GC.
Go’s memory management is handled by the runtime, which means a system call is not required for every memory allocation.
The algorithm used is the
TCMallocalgorithm, i.e.Thread-Caching Malloc. It organizes small-object allocation as a hierarchy of caches: each P uses an mcache to hold locally available mspans. Allocation prefers this local cache and obtains more spans from global structures such as mcentral when needed, reducing contention.
2. Heap Memory
When Go starts, it requests memory from the operating system. The memory is divided as follows:

- arena: the heap address space is managed in arena regions; the exact sizes and hierarchy depend on the architecture and runtime implementation.
- bitmap: records GC metadata such as pointer information for heap objects; the exact storage layout is a runtime implementation detail.
- spans: used to locate the corresponding mspan for a heap address.
3. Memory Management Unit

- mspan: the basic unit of memory management.
- Composed of consecutive pages.
- Divided into several objects according to the Size Class; each object can store one object.

4. Memory Allocator
4.1. mcache
Each P is bound to an mcache, which locally caches available mspan resources. It can therefore allocate directly to Goroutines; because an mcache is used by a single P, local allocation does not require locking.
4.2. mcentral
When a P’s mcache does not contain an appropriate mspan (that is, one of the required size), it obtains one from mcentral. mcentral is shared by multiple Ps, so access requires synchronization. It provides pre-split mspan resources to all mcaches. Each central maintains a global list of mspans of one specific size, including allocated and unallocated ones.
4.3. mheap
Represents all heap space held by a Go program. A Go program uses one global mheap object, _mheap, to manage heap memory.
When mcentral has no free mspan, it requests one from mheap. When mheap also has no resources, it requests new memory from the operating system.
5. Escape Analysis
6. Memory Allocation Flow
According to the result of escape analysis, allocate on the stack if possible; otherwise consider the heap. Objects are divided into three classes according to size: tiny objects (less than or equal to 16 B), normal objects (greater than 16 B and less than or equal to 32 KB), and large objects (greater than 32 KB).
- Objects larger than 32 KB are allocated directly from mheap.
- Objects <=16 B are allocated using mcache’s tiny allocator.
- Objects in
(16B,32KB]: first calculate the object’s size class, then allocate from the corresponding mspan in mcache.- If mcache has no mspan of the corresponding size, request one from mcentral.
- If mcentral has no mspan of the corresponding size, request one from mheap.
- If mheap also has no suitable mspan, request memory from the operating system.

7. References
- Illustrated Go Memory Allocation | qcrao
- Visualizing Go Memory Management | Tony Bai
- Go Memory Management Trilogy (1): Memory Allocation Principles - InfoQ
- How Go’s Memory Allocator Works | Go Language Design and Implementation
- Go: Memory Management and Allocation | by Vincent Blanchon | A Journey With Go | Medium
- 🚀 Demystifying memory management in modern programming languages | Technorage
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub