mcache的结构 #
mcache 是 Go 的线程缓存,对应于 TCMalloc 中的 Thread cache 结构。 mcache 会与线程绑定,每个 goroutine 在向 mcache 申请内存时,都不会与其他 goroutine 发生竞争。mcache 中会维护上述 68×2 种 spanClass 的 mspan 数组,存放在 mcache 的 alloc 中,包括 scan 以及 noscan 两个队列。 mcache 的主要结构如下:
type mcache struct {
...
tiny uintptr
tinyoffset uintptr
tinyAllocs uintptr
alloc [numSpanClasses]*mspan // spans to allocate from, indexed by spanClass
...
}
当 mcache 中的内存不够需要扩容时,需要向 mcentral 请求,mcentral 对应于 TCMalloc 中的 Central cache 结构。
Viewpoint #
From #
24 | GC实例:Python和Go的内存管理机制是怎样的?