NOTE
3.4 Memory Management
Golang memory regions and the relationship between runtime.MemStats, RSS, heap, stack, HeapSys, HeapInuse, HeapIdle, and HeapReleased.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Golang Memory Regions
1.1. Stack
1.2. Heap
1.3. Global Area - Static Global Variable Area
- Global variables + constants
- Allocation: when the process starts
- Reclamation: when the process exits, reclaimed by the operating system
1.4. Code Area
2. Golang runtime.MemStat
The RSS shown by top is the physical memory occupied by the Golang program.
The bytes occupied by a Golang program are managed by the Go Runtime + non-Go Runtime (cgo).
Memory managed by the Go Runtime is divided into heap and stack and can be viewed through runtime.MemStats.
Sys is similar to the RSS shown by top.
The Go Runtime divides Sys into two parts, heap and stack, i.e. Sys=HeapSys+StackSys.
The heap is also divided into two parts, used and unused, i.e. HeapSys=HeapInuse+HeapIdle.
For the unused heap, after subtracting the part returned to the OS, the remainder is retained by the runtime itself, i.e. HeapIdle-HeapReleased.
Go: Meaning and Differences of Memory Metrics Read by runtime.ReadMemStats(m *MemStats) - CSDN
Zemin Blog | Jekyll theme
# runtime.MemStats
General statistics
# Alloc = 2 937 593 488 same as HeapAlloc
# TotalAlloc = 21510 240 014 544 bytes allocated to heap objects. Cumulative value only increases and never decreases
# Sys = 4 239 444 952 bytes of memory obtained from the OS. Includes heap, stack, and internal structures, [similar to RSS shown by top]
# Lookups = 0
# Mallocs = 212712953221 total number of allocated heap objects. Cumulative value only increases and never decreases
# Frees = 212 702 641 433 total number of freed heap objects. Cumulative value only increases and never decreases
Heap allocation statistics
# HeapAlloc = 2 937 593 488 bytes allocated to heap objects. Includes reachable and unreachable objects
# HeapSys = 4 012 802 048 bytes of heap memory obtained from the OS. [HeapSys=HeapInuse+HeapIdle]
# HeapIdle = 973 094 912 bytes of unused spans. [HeapIdle-HeapReleased is memory that could be returned to the OS but is retained by the runtime to avoid reallocating it]
# HeapInuse = 3 039 707 136 bytes of spans in use
# HeapReleased = 27 262 976 bytes of memory returned to the OS
# HeapObjects = 10311788 number of allocated heap objects.
Stack memory statistics
# Stack = 13729792 / 13 729 792
# MSpan = 15704192 / 26263552
# MCache = 13888 / 163840
# BuckHashSys = 26252154
# GCSys = 152306400
# OtherSys = 7927166
# NextGC = 3897770816
# LastGC = 1649405542366123388
# PauseNs = [xxx]
# PauseEnd = [yyy]
# NumGC = 17446
# NumForcedGC = 0
# GCCPUFraction = 0.004723831578861987
# DebugGC = false
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub