linux-kernelmallocmemory-fragmentation

libc malloc vs linux kernel buddy allocator


Does malloc wory about internal fragmentation in linux kernel? For example when I want to allocate 5 pages, will malloc round up size to make it power of 2: 5->8 to avoid internal fragmentation in kernel, because linux kernel uses buddy system as page allocator.


Solution

  • At least for glibc, it doesn't really care about fragmentation in the kernel. It is mostly a "best-fit" allocator except for very small or very large allocations. Here is an extract from the comments near the top of glibc's "malloc.c":

    • Why use this malloc?

      This is not the fastest, most space-conserving, most portable, or most tunable malloc ever written. However it is among the fastest while also being among the most space-conserving, portable and tunable. Consistent balance across these factors results in a good general-purpose allocator for malloc-intensive programs. The main properties of the algorithms are:

      • For large (>= 512 bytes) requests, it is a pure best-fit allocator, with ties normally decided via FIFO (i.e. least recently used).
      • For small (<= 64 bytes by default) requests, it is a caching allocator, that maintains pools of quickly recycled chunks.
      • In between, and for combinations of large and small requests, it does the best it can trying to meet both goals at once.
      • For very large requests (>= 128KB by default), it relies on system memory mapping facilities, if supported.

      For a longer but slightly out of date high-level description, see http://gee.cs.oswego.edu/dl/html/malloc.html

    The glibc malloc implementation is fairly tuneable using the mallopt function or various environment variables to set various parameters, as described in the man page mallopt(3).