DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

Go Memory Allocator Visual Guide

原文链接

So how do we reduce the memory fragment? The answer to this question depends on the specific memory allocation algorithm, the underlying library use.

关键参数:thread memory and page heap

TCMalloc Go also divides Memory Pages into a block of 67 different classes Size.

引入mspan的概念

针对每一个logical processor都会有mcache

For each class size, there are two types.

  1. scan — Object that contains a pointer.
  2. noscan — Object that doesn’t contains a pointer.

Object allocation Flow

• Size > 32k is a large object, allocated directly from mheap.

• Size < 16B, using mcache’s tiny allocator allocation

• Size between 16B ~ 32k, calculate the sizeClass to be used and then use the block allocation of the corresponding sizeClass in mcache

• If the sizeClass corresponding to mcache has no available blocks, apply to mcentral.

• If there are no blocks available for mcentral, apply to mheap and use BestFit to find the most suitable mspan. If the application size is exceeded, it will be divided as needed to return the number of pages the user needs. The remaining pages constitute a new mspan, and the mheap free list is returned.

• If there is no span available for mheap, apply to the operating system for a new set of pages (at least 1MB).

So even for a simple go program virtual Space is around ~100 MB while RSS is just 696kB .

allocate memory