This is a creation in Article, where the information may have evolved or changed.
The entire memory management subsystem of the Go language consists of two main parts-the memory allocator and the garbage collector (GC). 11 Small long vacation in order to avoid my great country of the peak of the flow of people, so at home for 3 days to go to the memory splitter part of the code to study A, overall is very cool, I also learned a lot of things, this record share. The entire memory allocator is completely re-implemented based on the design of the Google home Tcmalloc, so if you want to see the memory allocator implementation of the go language, it is highly recommended to read the Tcmalloc introduction document first and then see the Go Runtime of the malloc.h source files annotated introduction, so basic about the design of the Go language memory allocator.
Go's memory allocator is mainly to solve the allocation of small objects and multi-threaded memory allocation problems. (The memory allocator mentioned later refers to the memory allocator implemented by the Go language). The memory allocator takes 32k as the size of the object, and memory objects less than or equal to 32k are considered small objects, and objects larger than 32k are large objects. Why is 32k a dividing line? I do not know this, I think it is an experience, if you know there are other more scientific reasons, please tell me.
The memory allocator will use a cache component for the allocated small object to be cached, as long as the allocation of small objects to the cache first query, there is free memory directly back to use, do not need to request memory to the operating system. The memory allocator of this cache component may exist at the same time, that is, each actual thread will have a cache component, so that the cache from the query, to obtain free memory when there is no need to lock, each time the small object request directly access to the cache of the corresponding thread. When we write the program, in fact, the vast majority of memory applications are less than 32k, belonging to small objects, so this memory allocation all go local cache, not to the operating system is obviously very efficient.
There is a cache, there must be a cache miss situation, the memory allocator in the face of Cache
not finding free memory, will try to Central
request a batch of small object memory from the local cache, where Central is a component shared by all threads, not exclusive, Therefore, a lock operation is required. We need to know that the central component is actually a cache, but it caches not a small object memory block, but rather a set of memory page (a page occupies 4k size). If there is no cached free memory page in Central, the Heap
memory is requested from within to populate Central. Of course, the operations on the heap also need to be locked, and all threads share a heap. The heap does not have cached memory, and of course it takes the memory directly from the operating system.
The memory allocation of small objects is achieved through a level-one cache, which is designed to increase the speed of memory allocation and avoid memory fragmentation. Large object memory allocations greater than 32k are not so troublesome, instead of querying each cache component one layer at a-level, apply directly to it Heap
. is about describing the entire memory allocator's component structure, the Cache, Central, and heap are three core components, and are also the objects that will be analyzed at a later point.
Implementation of the memory allocator I need to split into a number of articles to write, no energy to write, in fact, according to the component is easy to read and write. The following articles will be written in succession to each core component.
aside, in the world of basic system software, memory management is an eternal topic, so there are very good memory allocator implementations such as Tcmalloc and Jemalloc. It is said that Jemalloc in the case of more CPU cores, performance is better than Tcmalloc, but it is estimated that they are not comparable, subject design is similar. Jemalloc is also pure C code, should be very well worth a look. Do not know why, now for C + + projects, there is always a study of procrastination, there is no strong motivation to see the source of the first time.