Pidoku
3Memory

Archive

This learning material is in the archive. It is not currently maintained and has not been updated with the latest information, so tool versions, prices and dates may be out of date.

Memory

Module 35 lessons~4h 45m

Lessons, in order

About this module

Go manages memory for you. This module explains exactly how, because the difference between a service that spends 2% of its CPU on memory management and one that spends 30% is almost always a handful of decisions you can see once you know where to look.

#LessonThe question it answers
3.aStack and HeapWhere do values live, and what does each place cost?
3.bEscape AnalysisHow does the compiler decide, and how do I read its decision?
3.cThe AllocatorWhat happens, step by step, when the heap is asked for memory?
3.dThe Garbage CollectorHow is unused memory found and freed while the program keeps running?
3.eWriting Allocation-Aware CodeHow do I cut allocations where it matters?

When you finish you can predict whether a line of code allocates, explain what GOGC and GOMEMLIMIT do, read a GC trace, and remove the allocations from a hot path.

flowchart LR
  V["A value is created"] --> EA{"Escape analysis:<br/>can it outlive the function?"}
  EA -->|"no"| ST["Goroutine stack<br/>free: just move a pointer"]
  EA -->|"yes"| AL["Allocator<br/>size class, span, per-thread cache"]
  AL --> HP[("Heap")]
  HP --> GC["Garbage collector<br/>mark what is reachable, sweep the rest"]
  GC -->|"freed spans"| AL
  class V neutral
  class EA queue
  class ST compute
  class AL,HP,GC memory

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in