[PATCH v3] malloc: Mark pages with MADV_DONTNEED to shrink and grow rather than mremap()

Florian Weimer fweimer@redhat.com
Wed Aug 20 10:20:49 GMT 2025


* William Hunt:

> When reallocating mmap()ed chunks, use madvise() if shrinking to mark
> unused pages as MADV_DONTNEED, only making a call to mremap() on
> failure. Allow growing within MADV_DONTNEED pages for later calls to
> realloc under the original size. This improves the efficiency of
> shrinking large mmap()ed chunks, as madvise() is significantly faster
> than mremap(). It also provides robustness, as if mremap() fails when
> shrinking, another madvise() without the threshold check is attempted,
> and if this fails the pointer is returned as to avoid a potential
> malloc+memcpy+free failure. Since mremap() fragments the VAS by
> usually shrinking in-place, using madvise() will keep the VAS intact
> while freeing the physical frames backing the unused pages, such that
> they will be zero-filled before the first access. To avoid the
> process' VAS from being exhausted, create a threshold for the maximum
> relative size of an mmap()ed chunk that can be marked MADV_DONTNEED.

I'm not sure if this is a good idea.  I expect some (many?) applications
expect that realloc returns memory from shrinking large allocations to
the operating system.

Thanks,
Florian



More information about the Libc-alpha mailing list