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]
Shared-object boundary: for shared file or shmem mappings, later access repopulates from the backing object; MADV_DONTNEED does not punch a hole in it. MADV_REMOVE can change that object's bytes, while MADV_FREE concerns private anonymous memory. These contracts are not interchangeable. [1] [6]
Relations
- related
- Memory-mapped file, Copy-on-write (storage), Page cache, msync, POSIX_MADV_DONTNEED, POSIX_FADV_DONTNEED, MADV_FREE, MADV_REMOVE
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.
Citations
- Linux man-pages: madvise(2), MADV_DONTNEED semantics
- POSIX.1-2017: posix_madvise(), advice preserves memory-access semantics
- glibc 2.39: Linux posix_madvise implementation
- Linux man-pages: posix_madvise(3), glibc behavior and error convention
- Hesela: When don't need discards private edits
- Hesela: lazy discard is not backing-file removal