基于1.8.3版本,64位Linux操作系統(tǒng)
1、概述
Go內(nèi)存管理基于tcmalloc,使用連續(xù)虛擬地址,以頁(8k)為單位、多級(jí)緩存進(jìn)行管理;
在分配內(nèi)存時(shí),需要對(duì)size進(jìn)行對(duì)齊處理,根據(jù)best-fit找到合適的mspan,對(duì)未用完的內(nèi)存還會(huì)拆分成其他大小的mspan繼續(xù)使用
在new一個(gè)object時(shí)(忽略逃逸分析),根據(jù)object的size做不同的分配策略:
- 極小對(duì)象(size<16byte)直接在當(dāng)前P的mcache上的tiny緩存上分配;
- 小對(duì)象(16byte <= size <= 32k)在當(dāng)前P的mcache上對(duì)應(yīng)slot的空閑列表中分配,無空閑列表則會(huì)繼續(xù)向mcentral申請(qǐng)(還是沒有則向mheap申請(qǐng));
- 大對(duì)象(size>32k)直接通過mheap申請(qǐng)。
2、數(shù)據(jù)結(jié)構(gòu)
2.1 mspan
mspan并不直接擁有內(nèi)存空間,它負(fù)責(zé)管理起始地址為startAddr、級(jí)別(預(yù)分配頁個(gè)數(shù))為sizeclass的連續(xù)地址空間。
摘取重點(diǎn)內(nèi)容(下同)
type mspan struct {
//雙向鏈表
next *mspan
prev *mspan
//起始地址
startAddr uintptr
//包含多少頁
npages uintptr
//
stackfreelist gclinkptr
//有多少對(duì)象
nelems uintptr
//gc相關(guān)
sweepgen uint32
//級(jí)別
sizeclass uint8
//已被mcache使用
incache bool
//狀態(tài)
state mSpanState
}
2.2 mcache
Go為Per-thread (in Go, per-P)分配了mcache管理結(jié)構(gòu),所以對(duì)其操作是不需要鎖的,每個(gè)mcache有大小為67的mspan數(shù)組,存儲(chǔ)不同級(jí)別大小的mspan
type mcache struct {
tiny uintptr
tinyoffset uintptr
alloc [_NumSizeClasses]*mspan
stackcache [_NumStackOrders]stackfreelist
...
}
2.3 mcentral
mcentral集中管理,當(dāng)在mcache申請(qǐng)失敗的時(shí)候,會(huì)向mcentral申請(qǐng);mcentral有個(gè)關(guān)鍵方法cacheSpan(),它是整個(gè)分配的核心算法
type mcentral struct {
lock mutex
sizeclass int32
nonempty mSpanList
empty mSpanList
}
2.4 mheap
mheap是真實(shí)擁有虛擬地址的結(jié)構(gòu),同時(shí)擁有67個(gè)級(jí)別的mcentral,以及所有分配的mspan。
// _NumSizeClasses := 67
// _MaxMHeapList := 128
type mheap struct {
lock mutex
//size < 128 * 8k(1M)的可用mspanList
free [_MaxMHeapList]mSpanList
//size >= 128 * 8k(1M)的可用mspanList
freelarge mSpanList
busy [_MaxMHeapList]mSpanList
busylarge mSpanList
//gc相關(guān)
sweepgen uint32
sweepdone uint32
//所有的mspan
allspans []*mspan
//頁到span的查找表
spans []*mspan
//位圖
bitmap uintptr
bitmap_mapped uintptr
//真實(shí)申請(qǐng)的內(nèi)存起始地址
arena_start uintptr
//真實(shí)申請(qǐng)的內(nèi)存目前可用起始地址
arena_used uintptr
//真實(shí)申請(qǐng)的內(nèi)存結(jié)束地址
arena_end uintptr
//分級(jí)的mcentral
central [_NumSizeClasses]struct {
mcentral mcentral
pad [sys.CacheLineSize]byte
}
...
}
2.5 四者的關(guān)系示圖
3、初始化
初始化時(shí),Go向系統(tǒng)申請(qǐng)預(yù)留一段連續(xù)虛擬地址,大小(64位機(jī)器上)為512M(spans_mapped)+16G(bitmap_mapped)+512G(arena)
向系統(tǒng)申請(qǐng)預(yù)留的連續(xù)地址空間
+----------+-----------+-----------------------------+
| spans | bitmap | arena |
| 512M | 16G | 512G |
+----------+-----------+-----------------------------+
mheap的初始化在func mallocinit()中,而mallocinit被schedinit()調(diào)用
/src/runtime/proc.go
// The bootstrap sequence is:
//
// call osinit
// call schedinit
// make & queue new G
// call runtime·mstart
//
// The new G calls runtime·main.
mallocinit的邏輯為:
func mallocinit() {
// 0. 檢查系統(tǒng)/硬件信息,bala bala
// 1. 計(jì)算預(yù)留空間大小
arenaSize := round(_MaxMem, _PageSize)
bitmapSize = arenaSize / (sys.PtrSize * 8 / 2)
spansSize = arenaSize / _PageSize * sys.PtrSize
spansSize = round(spansSize, _PageSize)
// 2. 嘗試預(yù)留地址(區(qū)分不同平臺(tái) 略)
for i := 0; i <= 0x7f; i++ {
...
pSize = bitmapSize + spansSize + arenaSize + _PageSize
p = uintptr(sysReserve(unsafe.Pointer(p), pSize, &reserved))
}
// 3. 初始化部分mheap中變量
p1 := round(p, _PageSize)
spansStart := p1
mheap_.bitmap = p1 + spansSize + bitmapSize
mheap_.arena_start = p1 + (spansSize + bitmapSize)
mheap_.arena_end = p + pSize
mheap_.arena_used = p1 + (spansSize + bitmapSize)
mheap_.arena_reserved = reserved
// 4. 其他部分初始化,67個(gè)mcentral在這里初始化
mheap_.init(spansStart, spansSize)
_g_ := getg()
_g_.m.mcache = allocmcache()
}
mheap的初始化方法
// Initialize the heap.
func (h *mheap) init(spansStart, spansBytes uintptr) {
// 0. xxalloc.init
// 1. free、busy init
for i := range h.free {
h.free[i].init()
h.busy[i].init()
}
h.freelarge.init()
h.busylarge.init()
// 2. mcentral初始化
for i := range h.central {
h.central[i].mcentral.init(int32(i))
}
// 3. spans初始化
sp := (*slice)(unsafe.Pointer(&h.spans))
sp.array = unsafe.Pointer(spansStart)
sp.len = 0
sp.cap = int(spansBytes / sys.PtrSize)
}
mcentral的初始化比較簡單,設(shè)置自己的級(jí)別,同時(shí)將兩個(gè)mspanList初始化
而mcache的初始化在func procresize(nprocs int32) *p中,procresize也在schedinit()中調(diào)用,順序在mallocinit()之后,也就是說發(fā)生在mheap于mcentral的初始化后面
func procresize(nprocs int32) *p {
// 0. bala bala
// 1. 初始化P
for i := int32(0); i < nprocs; i++ {
pp := allp[i]
//初始化每個(gè)P的mcache
if pp.mcache == nil {
if old == 0 && i == 0 {
if getg().m.mcache == nil {
throw("missing mcache?")
}
pp.mcache = getg().m.mcache
} else {
pp.mcache = allocmcache()
}
}
}
}
而allocmcache比較簡單
func allocmcache() *mcache {
lock(&mheap_.lock)
c := (*mcache)(mheap_.cachealloc.alloc())
unlock(&mheap_.lock)
for i := 0; i < _NumSizeClasses; i++ {
c.alloc[i] = &emptymspan
}
c.next_sample = nextSample()
return c
}
至此,管理結(jié)構(gòu)mheap、67個(gè)mcentral及每個(gè)P的mcache都初始化完畢,接下來進(jìn)入重點(diǎn)--分配階段。
4、分配
前面說過,在分配對(duì)象內(nèi)存時(shí),根據(jù)對(duì)象的大小分為3個(gè)級(jí)別:極小、小、大;在這里我們假設(shè)關(guān)閉內(nèi)聯(lián)優(yōu)化,即沒有逃逸的存在。當(dāng)new一個(gè)對(duì)象時(shí),調(diào)用的是:
func newobject(typ *_type) unsafe.Pointer {
return mallocgc(typ.size, typ, true)
}
mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
dataSize := size
c := gomcache()
var x unsafe.Pointer
noscan := typ == nil || typ.kind&kindNoPointers != 0
if size <= maxSmallSize {
if noscan && size < maxTinySize {
// 極小對(duì)象
} else {
// 小對(duì)象
} else {
// 大對(duì)象
}
}
我們將針對(duì)這三類一一分析
- 首先是極小對(duì)象(<16byte)
off := c.tinyoffset
// 地址對(duì)齊
if size&7 == 0 {
off = round(off, 8)
} else if size&3 == 0 {
off = round(off, 4)
} else if size&1 == 0 {
off = round(off, 2)
}
//若之前tiny剩余空間夠用,則將極小對(duì)象拼在一起
if off+size <= maxTinySize && c.tiny != 0 {
// The object fits into existing tiny block.
x = unsafe.Pointer(c.tiny + off)
c.tinyoffset = off + size
c.local_tinyallocs++
mp.mallocing = 0
releasem(mp)
return x
}
//不若,則申請(qǐng)新的mspan
// Allocate a new maxTinySize block.
span := c.alloc[tinySizeClass]
v := nextFreeFast(span)
if v == 0 {
v, _, shouldhelpgc = c.nextFree(tinySizeClass)
}
x = unsafe.Pointer(v)
(*[2]uint64)(x)[0] = 0
(*[2]uint64)(x)[1] = 0
// 新申請(qǐng)的剩余空間大于之前的剩余空間
if size < c.tinyoffset || c.tiny == 0 {
c.tiny = uintptr(x)
c.tinyoffset = size
}
size = maxTinySize
其中nextFreeFast和nextFree先跳過去,因?yàn)樾?duì)象分配時(shí)也會(huì)使用到,之后一并分析;下面是極小對(duì)象分配的示意圖
先是有足夠剩余空間,那么對(duì)齊都直接利用(為了便于說明問題,tinyoffset用箭頭指向表示)
如果沒有足夠空間,則申請(qǐng)新的,若必要修正tiny及tinyoffset的值
- 接著分析小對(duì)象(16byte <= size <= 32k)
介于16b到32k之間大小的對(duì)象分配比較復(fù)雜,可以結(jié)合文末的流程圖,便于記憶
var sizeclass uint8
if size <= smallSizeMax-8 {
sizeclass = size_to_class8[(size+smallSizeDiv-1)/smallSizeDiv]
} else {
sizeclass = size_to_class128[(size-smallSizeMax+largeSizeDiv-1)/largeSizeDiv]
}
size = uintptr(class_to_size[sizeclass])
span := c.alloc[sizeclass]
v := nextFreeFast(span)
if v == 0 {
v, span, shouldhelpgc = c.nextFree(sizeclass)
}
x = unsafe.Pointer(v)
if needzero && span.needzero != 0 {
memclrNoHeapPointers(unsafe.Pointer(v), size)
}
首先計(jì)算申請(qǐng)對(duì)象的sizeclass,以此找到對(duì)應(yīng)大小的mspan;然后找到可用的地址。這里面有兩個(gè)重要的方法nextFreeFast和nextFree:
// nextFreeFast returns the next free object if one is quickly available.
// Otherwise it returns 0.
func nextFreeFast(s *mspan) gclinkptr {
//計(jì)算s.allocCache從低位起有多少個(gè)0
theBit := sys.Ctz64(s.allocCache)
if theBit < 64 {
result := s.freeindex + uintptr(theBit)
if result < s.nelems {
freeidx := result + 1
if freeidx%64 == 0 && freeidx != s.nelems {
return 0
}
//更新位圖、可用游標(biāo)
s.allocCache >>= (theBit + 1)
s.freeindex = freeidx
//根據(jù)result和s.elemsize起始地址計(jì)算v
v := gclinkptr(result*s.elemsize + s.base())
s.allocCount++
return v
}
}
return 0
}
重點(diǎn)是當(dāng)mcache沒有可用地址時(shí),通過nextFree向mcentral甚至mheap申請(qǐng)
func (c *mcache) nextFree(sizeclass uint8) (v gclinkptr, s *mspan, shouldhelpgc bool) {
s = c.alloc[sizeclass]
shouldhelpgc = false
freeIndex := s.nextFreeIndex()
if freeIndex == s.nelems {
// The span is full.
...
//重新填充當(dāng)前的mcache
systemstack(func() {
c.refill(int32(sizeclass))
})
shouldhelpgc = true
s = c.alloc[sizeclass]
freeIndex = s.nextFreeIndex()
}
...
...
}
向mcentral是通過refill來實(shí)現(xiàn)的
func (c *mcache) refill(sizeclass int32) *mspan {
_g_ := getg()
_g_.m.locks++
// 想mcentral歸還當(dāng)前的mspan
s := c.alloc[sizeclass]
if uintptr(s.allocCount) != s.nelems {
throw("refill of span with free space remaining")
}
if s != &emptymspan {
s.incache = false
}
// 獲取新的, mcentral.cacheSpan()重點(diǎn)分析
s = mheap_.central[sizeclass].mcentral.cacheSpan()
...
c.alloc[sizeclass] = s
_g_.m.locks--
return s
}
下面是一個(gè)很長的調(diào)用鏈路...
func (c *mcentral) cacheSpan() *mspan {
...
retry:
var s *mspan
//先從非空列表中找
for s = c.nonempty.first; s != nil; s = s.next {
...
goto havespan
}
//沒有則從空列表中找
for s = c.empty.first; s != nil; s = s.next {
...
goto retry
}
//實(shí)在沒有,那么申請(qǐng)吧
s = c.grow()
if s == nil {
return nil
}
havespan:
//
return s
}
// 由mcentral申請(qǐng)
func (c *mcentral) grow() *mspan {
...
s := mheap_.alloc(npages, c.sizeclass, false, true)
...
return s
}
//由mheap申請(qǐng)
func (h *mheap) alloc(npage uintptr, sizeclass int32, large bool, needzero bool) *mspan {
...
systemstack(func() {
s = h.alloc_m(npage, sizeclass, large)
})
...
return s
}
func (h *mheap) alloc_m(npage uintptr, sizeclass int32, large bool) *mspan {
...
s := h.allocSpanLocked(npage)
...
return s
}
//Best-fit算法
func (h *mheap) allocSpanLocked(npage uintptr) *mspan {
//先從128頁以內(nèi)(1M)的free列表中尋找
for i := int(npage); i < len(h.free); i++ {
list = &h.free[i]
...
}
// Best-fit 對(duì)于大對(duì)象申請(qǐng)也會(huì)用到這個(gè)方法
//基本思路是找到最小可以滿足需求的mspan,如果有多個(gè),選擇地址最小的
list = &h.freelarge
s = h.allocLarge(npage)
if s == nil {
//如果mheap也沒有空間了,向系統(tǒng)申請(qǐng)吧
if !h.grow(npage) {
return nil
}
s = h.allocLarge(npage)
if s == nil {
return nil
}
}
HaveSpan:
//轉(zhuǎn)移s
list.remove(s)
if s.inList() {
throw("still in list")
}
//對(duì)于申請(qǐng)到的內(nèi)存大于想要的,將其拆分,避免浪費(fèi)
if s.npages > npage {
...
h.freeSpanLocked(t, false, false, s.unusedsince)
s.state = _MSpanFree
}
return s
}
//向系統(tǒng)申請(qǐng)空間
func (h *mheap) grow(npage uintptr) bool {
//計(jì)算頁數(shù)
npage = round(npage, (64<<10)/_PageSize)
ask := npage << _PageShift
if ask < _HeapAllocChunk {
ask = _HeapAllocChunk
}
v := h.sysAlloc(ask)
...
}
func (h *mheap) sysAlloc(n uintptr) unsafe.Pointer {
...
// sysReserve調(diào)用mmap預(yù)留空間,至此調(diào)用鏈結(jié)束
p := uintptr(sysReserve(unsafe.Pointer(h.arena_end), p_size, &reserved))
...
}
- 最后是大對(duì)象
var s *mspan
shouldhelpgc = true
systemstack(func() {
// largeAlloc會(huì)調(diào)用mheap_.alloc,這個(gè)方法在小對(duì)象申請(qǐng)時(shí)已經(jīng)追蹤過
s = largeAlloc(size, needzero)
})
s.freeindex = 1
s.allocCount = 1
x = unsafe.Pointer(s.base())
size = s.elemsize
-
內(nèi)存分配的流程圖
流程圖
5、參考文獻(xiàn)
[1]. https://github.com/qyuhen
[2]. http://legendtkl.com/2017/04/02/golang-alloc/
[3]. https://tracymacding.gitbooks.io/implementation-of-golang/content/memory/memory_core_data_structure.html