MADV_DONTNEED
MADV_DONTNEED is a Linux memory-advice operation that discards a mapped range's current private contents so later accesses can repopulate it from its backing object or zero-filled anonymous pages.
Destructive advice: unlike ordinary access-pattern hints, native MADV_DONTNEED changes memory-access semantics. A private anonymous range reads as zero on subsequent access; do not use it while that range holds the only copy of needed data. The address must be page-aligned. [1]
File-backed control: Hesela's sequential Linux 6.8/glibc 2.39 probe filled a private file mapping with P, then observed the original A bytes after native discard. The entire backing file stayed unchanged. Private MS_SYNC and no-operation controls retained P. This is measured API behavior on one host, not a durability or performance result. [5]
Similar names: POSIX_MADV_DONTNEED must preserve access semantics and is a no-op in the pinned glibc implementation. It is not an interchangeable replacement for native discard. POSIX_FADV_DONTNEED instead takes a file descriptor and file-offset range. [2] [3]
Cache scope: discarding a mapping does not guarantee immediate release of every shared backing page, a globally cold cache or secure erasure. The native call reports errors through -1 and errno, unlike posix_madvise's direct error-number return. [1] [4]
Relations
- related
- Memory-mapped file, Copy-on-write (storage), Page cache, msync, POSIX_MADV_DONTNEED, POSIX_FADV_DONTNEED
Advantages
- Lets an application relinquish disposable private mapped contents without unmapping its address range.
Limitations
- Can destroy private edits that have not been preserved elsewhere.
- Interface and mapping mode matter; do not generalize Linux behavior to other operating systems.