Runtime Memory Allocator (mcache & mheap)
TCMalloc-derived memory architecture: size classes, spans, mcache, and mcentral.
“A bank with local cash drawers (mcache) for quick withdrawals, backed by the central vault (mcentral) for large requests.”
Key Concepts & Rules To Remember
- `mcache`: Per-P lock-free cache for small object allocations.
- `mcentral`: Shared cache of span classes.
- `mheap`: Allocates virtual memory directly from the OS.
1. Understanding Runtime Memory Allocator (mcache & mheap)
TCMalloc-derived memory architecture: size classes, spans, mcache, and mcentral. In Go, runtime memory allocator (mcache & mheap) is designed around clarity and high runtime efficiency.
// Size classes categorize allocations from 8 bytes to 32 KB without lock contentionHow Docker Manages GOMAXPROCS Inside Linux Containers (CFS Quotas)
By default, Go initializes `GOMAXPROCS` to the physical host CPU core count (e.g. 64 cores), even if the container is quota-limited to 2 CPUs. This created 64 OS threads fighting for 2 CPU quotas, causing heavy CFS throttling.
Integrated `go.uber.org/automaxprocs` to automatically read Linux CFS cgroup quotas at startup and configure `runtime.GOMAXPROCS` to match the exact container CPU limit.
import _ "go.uber.org/automaxprocs"
func main() {
// Automatically sets GOMAXPROCS to container CPU quota!
}Common Beginner Pitfalls & Mistakes
Knowledge Check
Verify your understanding with these interactive practice questions.
Quizzes
Quick checks for understanding
Multiple-choice with inline explanations—expand to see why.
Finished this lesson?
Mark it as complete to track your overall Go mastery.