This article focuses on the C memory management program, a few notable C memory management programs, including:
L Doug Lea malloc:Doug Lea malloc is actually a complete set of distribution programs, including Doug Lea's original distribution program, the GNU libc distribution program, and the Ptmalloc. Doug Lea's allocation program joins the index, which makes the search faster and can combine multiple unused blocks into one large block. It also supports caching so that the most recently freed memory can be reused more quickly. Ptmalloc is an extended version of Doug Lea Malloc that supports multithreading. The source code implementation of PTAMLLOC2 is analyzed in detail later in this article.
L BSD malloc:BSD malloc is implemented with the 4.2 BSD release, which is included in FreeBSD, which allocates objects from a pool of pre-sized objects. It has a size class for object sizes that have a size of 2 minus a constant for several powers. So, if you request an object of a given size, it simply assigns a matching size class to it. This provides a quick implementation, but may waste memory.
L Hoard: The goal of writing hoard is to make memory allocations very fast in a multithreaded environment. Therefore, its construction is centered on the use of locks, so that all processes do not have to wait for the allocated memory. It can significantly speed up multi-threaded processes that perform many allocations and recoveries.
L Tcmalloc:(thread-caching Malloc) is a member of Google-developed open source tools, "Google-perftools". Compared to malloc in the standard glibc library, Tcmalloc is much more efficient and faster in allocating memory. Tcmalloc is a general-purpose memory management program, integrating the advantages of memory pool and garbage collection, for small memory, by 8 of the whole number of times allocated, for large memory, by 4K of the whole number of times allocated. This has two advantages, one is the allocation of time faster, the kind of memory pool that provides dozens of kinds of choices, often to traverse through the various lengths, in order to select the appropriate species, and tcmalloc can simply do a few operations on the line. Second, the short-term gains are relatively large, allocating small memory to waste up to 7 bytes, large memory is 4 K. However, in the long run, the tcmalloc distribution is much more than other memory pools, which can lead to a low reuse rate. Tcmalloc also has an efficient mechanism to reclaim these idle memory. When a thread has more free memory, it is handed back to the process, and the process can provision it to other threads, and if some length is returned to the process, the other threads do not need it, and the process merges the lengths into memory pages and then cuts them into other lengths. If the process occupies more resources, it is said that it will not be handed back to the operating system. Periodic memory recovery to avoid the potential for exploding memory growth. Tcmalloc has a relatively high space utilization, with an additional 1% space. Try to avoid locking (once lock unlock about 100ns), use more efficient spinlock, adopt more reasonable granularity. Small chunks of memory and open memory allocations take different strategies: less than 32K is defined as small chunks of memory, small chunks of memory by size are divided into 8bytes,16bytes, ... , the 236Bytes is graded. A size that is not an integer multiple of a level is assigned an upward rounding. If the 13Bytes will be allocated according to 16Bytes, the allocation, first in the corresponding size of the thread of the idle linked list, if found can avoid the lock operation (the cache of this thread is only used by the thread itself). If it is not found, attempt to move some objects to the linked list of this thread from the appropriate level of the central memory area of the idle link table. If the corresponding linked list in the central memory area is also empty, the memory page is requested from the hub page allocator and then partitioned into the object store at that level. Bulk Memory Processing: Per page allocation, the size of each page is 4K, then memory by 1 pages, 2 pages, ..., 255 pages of size classification, the same size of memory blocks are also linked to the list.
Comparison of various C memory management program implementations
Strategy |
Allocation speed |
Recovery speed |
Local cache |
Ease of Use |
Versatility |
SMP Thread-friendliness |
GNU Malloc |
In |
Fast |
In |
Easy |
High |
In |
Hoard |
In |
In |
In |
Easy |
High |
High |
Tcmalloc |
Fast |
Fast |
In |
Easy |
High |
High |
As can be seen from the above table, the advantages of Tcmalloc is relatively large, tcmalloc advantages reflected in:
When allocating memory pages, we deal directly with the OS, while the common memory pools are typically based on other memory managers, and if the exact same memory management strategy, obviously tcmalloc the overhead of third-party memory management on performance and memory utilization. This happens because most of the coder in the memory pool do not know much about the OS
Most memory pools are allocated only, regardless of recycling. Of course, there is no recycling strategy, there are other ways to solve the problem. such as the coordination of resources between threads, modular cable module is generally a write multi-read, that is, only one thread to request, free memory, there is no thread to coordinate resources between, in order to avoid some chunks of large idle, the common practice is to reduce the type of memory block, increase the reuse rate, which may result in more internal fragmentation, If there is too much free memory, you can restart it directly.
As a common memory management library, Tcmalloc may not be able to exceed the dedicated, coarser memory pool. For example, in the application of the main use of 7 blocks of length, dedicated memory pool, you can only allocate these 7 lengths, so that there is no internal fragmentation. Or you can use statistics to set the length of the memory pool, or to make the internal fragments less.
So the meaning of tcmalloc is that, without any additional development costs, there is less overhead for memory, and it can theoretically prove that the optimal allocation will not be much better than the distribution of tcmalloc.
Source: http://blog.csdn.net/zgl07/article/details/8941870
Contrast glibc can be found that the idea of the two is almost the same, the difference is only in detail, the details of the difference in the project is also very important, at least in terms of performance and memory utilization Tcmalloc is a lot of lead. GLIBC in memory recycling do not do very well, a common problem, apply a lot of memory, and then released, just a small piece did not release, this time glibc must wait for this small piece also released, also the whole large release, in extreme cases, may cause a few grams of waste.
Common C Memory Management programs