Go memory management - from TCMalloc to go memory management

TCMalloc

concept

  • Page: A page is a fixed-size memory block composed of consecutive physical addresses
  • Span: It is a fixed-size area composed of a series of consecutive pages
  • ThreadCache : The cache of each thread, each cache contains multiple linked lists , and the memory block size of each linked list is the same, which is equivalent to classifying the memory block size
  • CentralCache : A shared cache for all threads. The cache contains the same number of linked lists as Threadcache. When the memory block of ThreadCache is insufficient, memory blocks can be obtained from CentralCache
  • PageHeap : The abstraction of heap memory . PageHeap also stores several linked lists. When CentralCache has insufficient memory, it will obtain free memory span from PageHeap. When CentralCache has too much memory, it will put the free memory block back into PageHeap.

allocation process

TCMalloc divides objects into 3 types, small object size: 0-256KB, medium object size: 257-1MB
, large object size: >1MB

Small object allocation order:

ThreadCache -> CentralCache -> HeapPage

Object allocation order in:

Select directly in PageHeap

Large object allocation:

Select an appropriate number of pages from the large span set to form a span

Go memory management

concept

  • Page : Consistent with TCMalloc
  • Span : Consistent with TCMalloc
  • mcache : Similar to ThreadCache
  • mcentral : Similar to CentralCache in TCMalloc, but each level of span in mcentral will store two linked lists, one is used to store objects containing pointers, and the other is used to store objects that do not contain pointers
  • mheap: mheap is similar to PageHeap in TCMalloc, but mheap is stored in a tree structure. TCMlloc uses a linked list, which stores two binary sorting trees, sorted by the number of span pages, and one is free, which saves spans that are free and non-garbage Recycled spans, one is scav, which saves spans that are idle and have been garbage collected
  • object size : the object size of the requested memory
  • size class: it is the level of size
  • span class: refers to the span level
  • num of page: referred to as npage, the number of Pages

memory allocation

In GO, objects are divided into small objects and large objects (>32KB), and small objects are divided into tiny objects (1Byte~16Byte), and other small objects

Small object memory allocation:

The process of finding a span is as follows:

  1. Calculate the memory size required by the object
  2. According to the size to size class mapping, calculate the required size class
  3. Calculate the span class based on the size class and whether the object contains pointers
  4. Get the span pointed to by the span class
  5. Allocate object space from span , find the first available memory block from span
  6. mcache applies for a span from mcentral . mcentral, like mcache, has 134 span class levels from 0 to 133, but each level saves 2 span lists, that is, 2 span linked lists. nonempty: spans in this linked list, all spans Both have at least 1 free object space. These spans are added to the linked list when mcache releases spans. empty: The spans in this linked list, all spans are not sure whether there is free object space in it. When a span is handed over to mcache, it will be added to the empty list. Process: When mcache applies for a span from mcentral, mcentral will first search for a span that meets the conditions from nonempty, and if not found, then search for a span that meets the conditions from emtpy, and then hand the found span to mcache.
  7. Mecentral applies for span from mheap : mcentral needs to provide mheap with the required number of memory pages and span class level, and then it searches for available spans from free first. If not found, scav is searched for available spans. If it has not been found, it will apply for memory from the OS, and then search the two trees again, and it will definitely find the span. If the found span is larger than the required span, divide the span into 2 spans, one of which is exactly the required size, add the remaining span to free, then set the basic information of the required span, and then Give it to mcentral.

large object allocation

Same as mcentral applying for memory from mheap

Guess you like

Origin blog.csdn.net/csxylrf/article/details/130490052