Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand TCMalloc

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares with you is about how to understand TCMalloc. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

As the core algorithm of memory management in Go language, TCMalloc is a very important step to understand and master the memory management of Go. It mainly introduces what TCMalloc looks like.

Overview of TCMalloc

TCMalloc, whose full name is Thread-Caching Malloc, is a memory allocator developed by Google and is used in many projects. For example, a similar algorithm is used for memory allocation in Golang. It has the basic characteristics of a modern memory allocator: it is resistant to memory fragmentation and can be scale in multi-core processors.

1. TCMalloc allocates memory faster than glibc 2.3.

2.TCMalloc reduces locking mechanisms for multithreaded programs, operations without locks for small objects, and more efficient spin locks (spinlock) for large objects.

3.TCMalloc is more efficient in dealing with small objects.

1.TCMalloc allocates a thread-local Thread Cache to each thread, which is used for memory allocation of small objects (less than 32K). When necessary, objects are moved from Central Heap (Note: this is shared by multiple threads and need to be locked and unlocked during operation) to Thread Cache.

two。 The garbage collector (garbage collections) periodically migrates storage from Thread Cache (default is 2m) to Central Heap for garbage collection.

3. For large objects (greater than 32K), memory is allocated directly from Central Heap according to the page hierarchy.

Allocation of small objects

Each small object corresponds to the 170class clusters that have been allocated, and the 170clusters are allocated according to size, for example: 8, 16, 32, 48...256KB. The corresponding relationship between the assigned objects and these class clusters is rounded, for example, 961 to 1024 bytes will correspond to the 1024 class cluster.

Each class cluster is followed by a freelist,freelist, which is actually a linked list, in which the elements are allocated according to the size of the class cluster, and the first 8 bytes of the element are used as pointers to subsequent nodes, stringing the entire node together. As shown in the following figure:

The process for assigning small objects is as follows:

Step 1: first, find the corresponding class cluster based on the size of the object, such as the location of 32 bytes.

Step 2: look in the corresponding freelist in the thread's class cluster, and if there are no assigned elements in the freelist, select the element and return the object (Note: look it up from beginning to end).

Step 3: if the freelist corresponding to this class cluster has been allocated, 1) first take some objects from the central Heap to thread Cache;2) and then follow the steps in step 2 from this freelist to allocate the objects.

Step 4: if the freelist in central Heap is also empty, it will be operated as follows: 1) first assign a running page to central Heap; 2) divide the running page into a group of objects according to the corresponding class-size, and put these objects under the freelist of central Heap; 3) if you operate according to step 3, an object will be allocated successfully at last.

Allocation of large objects

Large objects refer to objects larger than 32K, and their allocation is done in central Heap. For large objects, it is defined according to the page size. Generally, a page is 8K. If the object is larger than one page, several pages will be assigned to complete the data allocation. As shown in the following figure:

Multiple consecutive page will form a span, and the starting page number and the number of page will be recorded in the span. When allocating, large objects will directly allocate span, and small objects will allocate objects from span. The corresponding relationship between page and span is shown in the following figure. From the figure, we can see that span a corresponds to 2 page,span b, one page,span c, 5 page,span d and 3 page.

For large objects, they are managed through pageHeap, and the structure is as follows:

The allocation process for large objects is as follows:

Step 1: first of all, determine how many pages this large object needs. Assuming n, it will look up the freelist corresponding to n page to see if there is any span that can be allocated, and if so, return it directly.

If there is no span for step 2: n page, it will find out if there is a suitable one in the span corresponding to nail1 page. If so, the corresponding span will be divided into n and 1 page span, the span of n pages will be returned, and the span of 1 page will be put into the freelist of 1 page.

Step 3: if you can't find it from 1 to 128page, look it up in large page, which is a little more complicated.

Step 4: if the large page is still not found, it will apply for 128page from the operating system, and return the required n-page span at the same time, and give the rest of the span to other page for next use.

The release process for large objects is as follows:

Step 1: check its left and right neighbors based on the current span to be freed. If they are free, remove them from the freelist, merge them into the same span, and hang them in the freelist.

Step 2: check to see if it needs to be released to the operating system, and if so, to the operating system.

The above is how to understand TCMalloc, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report