Tool and Resource Center

Help developers work more efficiently and provide tools and resources around the developer lifecycle

Developer.aliyun.com/tool?spm=a1…

An overview,

Memory management is a big part of any programming language, and Golang is no exception. Go borrows from Google’s TCMalloc, which is a high-performance memory allocator for c++. Its core idea is the memory pool + multi-level object management, can accelerate the allocation speed, reduce the resource competition.

2. Infrastructure

The main object structures used for memory management in Go are:

Mheap, mSPAN, Arenas, McEntral, and McAche.

Among them, MSPAN is an infrastructure structure, when allocating memory, basically in its unit.

McAche, McEntral, and Mheap act as memory pools and are pre-allocated. When an object of the corresponding size needs to be allocated, it is requested at their level first. If the memory pool at this layer is insufficient, the memory pool will be allocated in the following order:

McAche -> McEntral -> Mheap -> Operating system

mspan&&arenas

Let’s start with the mSPAN infrastructure. First, when Go initializes the program, it will divide the virtual memory allocated into the following three parts:

Arenas are dynamically allocated heap areas, which manage allocated memory in 8K pages.



However, the “page” unit is still too thin, so we abstracted the mspan layer, which represents a contiguous set of pages.

Mspan records the start and end addresses, the number of pages, and the type specifications for this set of consecutive pages.

There are 67 types of mspan specifications, each of which is defined with a fixed size. When an object needs to allocate memory, the mspan of the appropriate size is selected for the object.

Class 1 2 3 45 6 ·· 63 64 65 66 bytes 8 16 32 48 64 80 ·· 24576 27264 28672 32768Copy the code

For example, if memory is allocated for an object of size 30B, class 3 is selected, that is, mspan is allocated for size 32B. This allocation method is similar to the partner algorithm Linux uses for memory allocation and can effectively reduce memory fragmentation.

The bitmap area is used to mark which addresses in the Arena area hold objects, GC scan information, and object pointer information.

Overall, the SPANS and Bitmap regions can be viewed as metadata information on arenas regions to aid memory management.

mheap && mcentral

Mheap is a global object in Go that manages memory allocation for objects larger than 32K.

McEntral maintains mSPAN of various specifications. When its child McAche runs out of memory, it requests an MSPAN from McEntral.

Because McEntral has mSpans for each specification type, there is no concurrent contention issue when there are allocation requests for different specifications. A lock is added only when mspan concurrent requests of the same type are allocated.

mcache

McAche is the local memory pool provided to P. (see golang Important Knowledge: Golang Scheduling for GPM model), because only one Goroutine is executed on P at a time. So there is no competition for allocating memory.

McAche also has a tiny allocator that allocates multiple objects across an 8byte mspan when allocating smaller elements: <= 16B, for better memory use.

Three, the overall process

  • When an object greater than 32K is allocated, it is allocated from the Mheap.
  • If the object to be allocated is less than or equal to 32K but greater than 16B, the object is allocated from McAche on P. If McAche does not have memory, the object is allocated from McEntral. If mheap does not have memory, the object is allocated from Mheap. The system allocates memory from the OS.
  • When the object to be allocated is less than or equal to 16B, it is allocated from the micro allocator on the McAche.