Microsoft opened the code for the mimalloc memory allocation system

Microsoft has opened a library under the MIT license mmalloc from the implementations of the memory allocation system, originally created for the runtime components of languages coca и Lean. Mimalloc is adapted for use in typical applications without changing their code and can act as a transparent replacement for the malloc function. Work is supported on Windows, macOS, Linux, BSD and other Unix-like systems.

The key feature of mimalloc is its compact implementation (less than 3500 lines of code) and very high performance. IN tests carried out mimalloc outperformed all competing memory allocation libraries, including jemalloc, tcmalloc, snmalloc, rpmalloc и Hoard.

To evaluate the performance, a set of already existing standard tests In some tests, mimalloc is several times faster than other systems, for example, in the test of object migration between different threads, mimalloc turned out to be more than 2.5 times faster than tcmalloc and jemalloc. At the same time, in most tests, lower memory consumption is also observed; in some situations, memory consumption can be reduced by 25%.

Microsoft opened the code for the mimalloc memory allocation system

High performance is achieved mainly through the use of free list sharding. Instead of a single large list, mimalloc uses a series of smaller lists, each bound to a page of memory. This approach reduces fragmentation and improves data localization in memory. A memory page is a grouped set of blocks of similar size. On 64-bit systems, the page size is typically 64 KB. If there are no occupied blocks left in the page, it is completely freed and memory is returned to the operating system, which reduces memory costs and fragmentation in long-running programs.

The library can be included at the linking stage or loaded for an already built program ("LD_PRELOAD=/usr/bin/libmimalloc.so myprogram"). The library also provides API for integrating functionality into runtime and fine-tuning behavior, for example, for connecting handlers for delayed memory release and monotonous increase in reference counters. It is possible to create and use several "heaps" (heap) in the application for distribution over different areas of memory. In particular, it is possible to release the entire heap, without enumeration and separate release of the objects placed in it.

It is possible to build the library in safe mode, in which special memory check pages (guard-page) are substituted at the block boundary, and block distribution is randomized and lists of freed blocks are encrypted. Such measures allow blocking most typical heap buffer overflow exploitation techniques. When you enable safe mode, performance is reduced by about 3%.

Of the features of mimalloc, it is also noted that it is not susceptible to bloat problems with large fragmentation. In the worst case scenario, memory consumption increases by 0.2% for metadata and can be as high as 16.7% for shared memory. To avoid conflicts when accessing resources, mimalloc uses only atomic operations.

Source: opennet.ru

Add a comment