Go manages memory for you: a heap allocator plus the GC. Most of the time you never touch malloc yourself. Below: the pool allocator in the runtime (Go 1.10.3) and how the pieces connect.
The design follows tcmalloc (thread-caching malloc): per-thread caches for small objects (≤32 KiB), then central lists, then the heap, then the OS. Tiered caches shrink lock contention and fragmentation.
Below: how memory is carved up, the main structs, then the allocation path in source.
Memory layout
At init the runtime maps one contiguous region and splits it into spans metadata, bitmap, and arena (runtime/malloc.go).
On 64-bit (pointer size 8 bytes):
Arena — the heap pool (~512 GiB virtual).
Bitmap — which arena words hold objects and whether they contain pointers; 1 byte covers 4 pointer slots (2 bits per pointer) → ~16 GiB metadata.
Spans — one pointer slot per 8 KiB heap page → ~512 MiB.
Data structures
mspan
A run of contiguous pages for a given size class — the basic allocation unit. The stack pool also chains mspan values (runtime/stack.go) to cut stack alloc cost for goroutines.
type mspan struct { next *mspan // next span in list, or nil if none prev *mspan // previous span in list, or nil if none list *mSpanList // For debugging. TODO: Remove.
startAddr uintptr // address of first byte of span aka s.base() npages uintptr // number of pages in span
manualFreeList gclinkptr // list of free objects in _MSpanManual spans
freeindex uintptr nelems uintptr // number of object in the span. allocCache uint64 allocBits *gcBits gcmarkBits *gcBits
sweepgen uint32 divMul uint16 // for divide by elemsize - divMagic.mul baseMask uint16 // if non-0, elemsize is a power of 2, & this will get object allocation base allocCount uint16 // number of allocated objects spanclass spanClass // size class and noscan (uint8) incache bool // being used by an mcache state mSpanState // mspaninuse etc needzero uint8 // needs to be zeroed before allocation divShift uint8 // for divide by elemsize - divMagic.shift divShift2 uint8 // for divide by elemsize - divMagic.shift2 elemsize uintptr // computed from sizeclass or from npages unusedsince int64 // first time spotted by gc in mspanfree state npreleased uintptr // number of pages released to the os limit uintptr // end of data in span speciallock mutex // guards specials list specials *special // linked list of special records sorted by offset. }
Notable fields:
freeindex — index of next free slot in [0, nelems); slots before it are allocated.
allocBits — bitmap of free slots (1 = free).
allocCache — complemented bits from freeindex for fast ctz scanning (sys.Ctz64).
spanclass — size class + noscan flag.
elemsize — object size for size class; page multiple for large spans.
mcache
Per-P cache (only one M holds a given P at a time) — no lock for small allocs. alloc holds 67 *mspan buckets (local cache).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
type mcache struct { next_sample int32 local_scan uintptr
central [numSpanClasses]struct { mcentral mcentral pad [sys.CacheLineSize - unsafe.Sizeof(mcentral{})%sys.CacheLineSize]byte }
spanalloc fixalloc cachealloc fixalloc // ... }
freelarge — treap of large free spans (best-fit; see mgclarge.go).
spans — page index → mspan.
central — array of mcentral.
fixalloc hands out fixed-size metadata objects (mspan, mcache, treap nodes) without fragmentation:
1 2 3 4 5 6 7 8 9 10 11
type fixalloc struct { size uintptr first func(arg, p unsafe.Pointer) arg unsafe.Pointer list *mlink chunk uintptr nchunk uint32 inuse uintptr stat *uint64 zero bool }
Allocation flow
Allocate
> 32 KiB — large object from mheap.
< 16 B — tiny allocator in mcache (packed tiny objects, with alignment rules 8/4/2/1 byte). Some waste, faster access.
16 B … 32 KiB — pick sizeClass, alloc from mcache.alloc[class].
mcache miss → mcentral → mheap (best-fit span, split remainder) → OS (≥1 MiB pages).
Free
Object returns to mcache; may flush to mcentral then mheap. Sweep: mspan.sweep in runtime/mgcsweep.go.
func largeAlloc(size uintptr, needzero bool, noscan bool) *mspan { if size+_PageSize < size { throw("out of memory") } npages := size >> _PageShift if size&_PageMask != 0 { npages++ }
deductSweepCredit(npages*_PageSize, npages)
s := mheap_.alloc(npages, makeSpanClass(0, noscan), true, needzero) if s == nil { throw("out of memory") } s.limit = s.base() + size heapBitsForSpan(s.base()).initSpan(s) return s }
Summary
Go tiers allocation: tiny bump buffer, size-class spans in per-P caches, central lists, then heap pages from the OS. Fixed size classes limit fragmentation; caching amortizes locks and syscalls.
Tiny (<16 B): mcache tiny allocator
Small (16 B–32 KiB): size class + mcache / mcentral