malloc_trim cannot return a freed heap top under 2 MiB on aarch64 (2.43)

hi@joel.id hi@joel.id
Mon Nov 23 09:46:40 GMT 2026


Since "malloc: Enable 2MB THP by default on Aarch64" (321e1fc73f53) and
"malloc: Improve thp_init" (1c588a2187a4), thp_init sets
mp_.thp_pagesize to DEFAULT_THP_PAGESIZE (2 MiB) on aarch64 whatever
__malloc_thp_mode answers, including malloc_thp_mode_not_supported on a
kernel built without CONFIG_TRANSPARENT_HUGEPAGE. sysmalloc then grows
the main heap to 2 MiB boundaries, and systrim releases the top chunk
only in 2 MiB units (ALIGN_DOWN (top_area - pad, mp_.thp_pagesize)).
mtrim releases the pages of free bin chunks but not those of the top
chunk. So malloc_trim (0) returns 0 and leaves a freed top chunk of up
to 2 MiB resident, pages the process touched once and no longer uses.

Observed with glibc 2.43 on aarch64, Linux 7.2, 16 KiB pages,
CONFIG_TRANSPARENT_HUGEPAGE not set:

  /* K kB in 16 kB blocks above a pin, touched, then freed from the
     top down so they join the top chunk; measured without allocating. */
  #include <fcntl.h>
  #include <malloc.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  static char buf[1 << 20];
  static void heap(const char *what, int k) {
    int fd = open("/proc/self/smaps", O_RDONLY); size_t u = 0; ssize_t r;
    while ((r = read(fd, buf + u, sizeof buf - 1 - u)) > 0) u += r;
    close(fd); buf[u] = 0;
    char *h = strstr(buf, "[heap]"); char *s = h;
    while (s > buf && s[-1] != '\n') s--;
    unsigned long lo = strtoul(s, &s, 16), hi = strtoul(s + 1, 0, 16);
    long rss = strtol(strstr(h, "Rss:") + 4, 0, 10);
    char o[200];
    int m = snprintf(o, sizeof o, "K=%d %-5s size %lu kB rss %ld kB\n",
                     k, what, (hi - lo) / 1024, rss);
    write(1, o, m);
  }
  static void *r[4096];
  int main(int argc, char **argv) {
    int kb = atoi(argv[1]), k = kb / 16;
    void *pin = malloc(100); memset(pin, 1, 100);
    for (int i = 0; i < k; i++) { r[i] = malloc(16000); memset(r[i], 1, 16000); }
    heap("peak", kb);
    for (int i = k - 1; i >= 0; i--) free(r[i]);
    heap("freed", kb);
    int rc = malloc_trim(0);
    heap("trim", kb);
    char o[64]; write(1, o, snprintf(o, sizeof o, "rc=%d\n", rc));
    return 0;
  }

  $ ./a.out 800
  K=800 peak  size 1952 kB rss 784 kB
  K=800 freed size 1952 kB rss 784 kB
  K=800 trim  size 1952 kB rss 784 kB
  rc=0
  $ ./a.out 1500
  K=1500 peak  size 1728 kB rss 1456 kB
  K=1500 freed size 1728 kB rss 1456 kB
  K=1500 trim  size 1728 kB rss 1456 kB
  rc=0

With thp_pagesize 0, systrim would release such a top in page units.
For a small long-running process this keeps its heap's high-water mark
resident: in a test run, a daemon of ours with 301 kB in use keeps 912 kB
of resident heap after malloc_trim (0), 588 kB of it the freed top.

Either change would fix it on such a kernel: leave thp_pagesize at 0 when
__malloc_thp_mode answers malloc_thp_mode_not_supported (THP alignment
buys nothing there), or have mtrim madvise (MADV_DONTNEED) the whole
pages of the top chunk that systrim leaves.



More information about the Libc-alpha mailing list