Back to Concepts Roadmap
🟩 Level 14 — Internals Deep Dive
Internals

Runtime Memory Allocator (mcache & mheap)

TCMalloc-derived memory architecture: size classes, spans, mcache, and mcentral.

Real-World Analogy (Mental Model)

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.
Step-by-Step Code

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.

example.goGo 1.24+
// Size classes categorize allocations from 8 bytes to 32 KB without lock contention
Battle-Tested Production Case Study

How Docker Manages GOMAXPROCS Inside Linux Containers (CFS Quotas)

Docker Architecture
Production Challenge

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.

Architectural Solution

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.

production_pattern.goEliminated CPU throttling, reducing P99 latency by 30% without changing application logic.
import _ "go.uber.org/automaxprocs"

func main() {
    // Automatically sets GOMAXPROCS to container CPU quota!
}

Common Beginner Pitfalls & Mistakes

Mistake: Misusing runtime memory allocator (mcache & mheap) without understanding its memory or concurrency semantics.
✅ Correct Way: Always follow standard Go idioms and verify with tests.
Self Assessment

Knowledge Check

Verify your understanding with these interactive practice questions.

Quizzes

Quick checks for understanding

Multiple-choice with inline explanations—expand to see why.

What is the primary concept behind Runtime Memory Allocator (mcache & mheap)?

Finished this lesson?

Mark it as complete to track your overall Go mastery.