go内存管理


内存管理组件

mspan

runtime.mspan 是 Go 语言内存管理的基本单元

每个 runtime.mspan 都管理 npages 个大小为 8KB 的页

runtime.spanClass 是 runtime.mspan 的跨度类,它决定了内存管理单元中存储的对象大小和个数

线程缓存(mcache)

runtime.mcache 是 Go 语言中的线程缓存,它会与线程上的处理器一一绑定,主要用来缓存用户程序申请的微小对象。每一个线程缓存都持有 68 * 2 个 runtime.mspan,这些内存管理单元都存储在结构体的 alloc 字段中。其中68个scan, 另外68个noscan

中心缓存(mcentral)

runtime.mcentral 是内存分配器的中心缓存,与线程缓存不同,访问中心缓存中的内存管理单元需要使用互斥锁

每个中心缓存都会管理某个跨度类的内存管理单元,它会同时持有两个 runtime.spanSet,分别存储包含空闲对象和不包含空闲对象的内存管理单元。

页堆(mheap)

runtime.mheap 是内存分配的核心结构体,Go 语言程序会将其作为全局变量存储,而堆上初始化的所有对象都由该结构体统一管理,该结构体中包含两组非常重要的字段,其中一个是全局的中心缓存列表 central,另一个是管理堆区内存区域的 arenas 以及相关字段。

页堆中包含一个长度为 136 的 runtime.mcentral 数组,其中 68 个为跨度类需要 scan 的中心缓存,另外的 68 个是 noscan 的中心缓存

相关资料

  1. 内存分配器
  2. 【Golang】这个内存对齐呀!?
  3. 从源码讲解 golang 内存分配
  4. 详解Go中内存分配源码实现
  5. 🚀 Visualizing memory management in Golang
  6. A visual guide to Go Memory Allocator from scratch (Golang)
  7. TCMalloc : Thread-Caching Malloc
  8. Golang 内存管理
  9. 带你领略Go源码的魅力—-Go内存原理详解
  10. The Go Memory Model
  11. Go: Memory Management and Allocation
  12. 可视化Go内存管理
  13. 图解Go内存分配器
  14. go 内存管理
  15. Go runtime剖析系列(一):内存管理
  16. 内存管理
  17. Go 内存管理
  18. go夜读-图解 Go 之内存对齐
  19. 栈的运行速度比堆快? 栈堆详解

https://xie.infoq.cn/article/ee1d2416d884b229dfe57bbcc

https://juejin.cn/post/6844904005215207432

https://juejin.cn/post/6844903795739082760

https://cloud.tencent.com/developer/article/1771373

https://www.jianshu.com/p/1f0a0ec2d661

https://www.techug.com/post/manual-memory-management-in-go.html

https://golang.design/under-the-hood/zh-cn/part2runtime/ch07alloc/

https://wudaijun.com/2019/09/go-performance-optimization/

https://gfw.go101.org/article/memory-block.html

http://blog.newbmiao.com/2018/08/20/go-source-analysis-of-memory-alloc.html

https://ilifes.com/golang/memory-alloc/

https://www.diglog.com/story/1035817.html

https://gohalo.me/post/golang-concept-memory-management-module-introduce.html

http://www.djaigo.com/golang/golang-nei-cun-guan-li.html

https://omen.ltd/archives/12/

https://www.happyxhw.cn/memory/

http://www.zhangfuguan.top/2020/08/16/go%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/index.html

https://mp.weixin.qq.com/s/rydO2JK-r8JjG9v_Uy7gXg

https://andblog.cn/?p=2887

https://www.bookstack.cn/read/GoExpertProgramming/chapter04-4.1-memory_alloc.md

Understanding Allocations: the Stack and the Heap - GopherCon SG 2019
GopherCon UK 2018: Andre Carvalho - Understanding Go’s Memory Allocator
The Go Memory Model: GoSF Meetup, 1/23/19

https://tonybai.com/2020/03/10/visualizing-memory-management-in-golang/

https://tonybai.com/2020/02/20/a-visual-guide-to-golang-memory-allocator-from-ground-up/

https://github.com/golang/proposal/blob/master/design/12800-sweep-free-alloc.md

https://docs.google.com/document/d/1un-Jn47yByHL7I0aVIP_uVCMxjdM5mpelJhiKlIqxkE/edit#heading=h.bvezjdnoi4no

https://medium.com/a-journey-with-go/go-how-does-the-goroutine-stack-size-evolve-447fc02085e5

https://docs.google.com/document/d/1wAaf1rYoM4S4gtnPh0zOlGzWtrZFQ5suE8qr2sD8uWQ/pub


文章作者: 金龙
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 金龙 !
  目录