MADV_FREE does not immediately replace a private anonymous page with zeros. It marks eligible pages as disposable, allowing Linux to reclaim them later. Until reclaim actually occurs, existing bytes can remain observable. A write before reclaim cancels the disposable state for the affected page.
That timing makes MADV_FREE distinct from advice that immediately changes the process-visible state of a range. It is a lazy reclamation contract: the application declares that old contents are expendable, while the kernel chooses when physical memory is recovered.
The mapping remains valid after the call
A successful madvise(addr, length, MADV_FREE) leaves the virtual address range mapped. The call does not behave like munmap(), and it does not create an inaccessible hole in the address space.
void *p = mmap(NULL, length,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (p == MAP_FAILED) {
return -1;
}
if (madvise(p, length, MADV_FREE) == -1) {
return -1;
}MADV_FREE applies to private anonymous pages. Ranges containing file mappings, MAP_SHARED mappings, Huge TLB mappings, or VM_PFNMAP areas are not valid targets for this advice.
The application can continue addressing the range after the call. The important change is the kernel’s permission to discard the old page contents.
Reclaim is deferred
The kernel may postpone freeing marked pages until memory pressure makes reclamation useful. This means a read soon after MADV_FREE can still return the bytes that existed before the call.
Those bytes no longer have durable application semantics. Once Linux reclaims a marked page, its stale contents are lost. A later reference receives a zero-fill-on-demand page rather than the discarded data.
This produces two valid states after the call:
before reclaim: old bytes may still be present
after reclaim: a later reference observes a zero-filled pageCode must not use a successful MADV_FREE as a point after which reading the old value is guaranteed to return zero. The transition to zero-filled memory is tied to reclamation, not to completion of madvise().
A subsequent write cancels disposal
A write to a page that has been marked with MADV_FREE but has not yet been reclaimed makes that page dirty again. Linux can no longer discard that newly written state under the earlier advice.
This property permits allocators and caches to mark unused anonymous memory as expendable without first forcing immediate physical reclamation. If the memory is reused quickly, a write restores ordinary dirty-page semantics. If it remains unused, the kernel has reclaimable memory available when pressure rises.
The behavior is page-oriented even when the application manages smaller logical objects. Marking a range that contains still-needed data creates a correctness risk because reclaim can discard the entire affected page contents.
MADV_FREE and MADV_DONTNEED expose different boundaries
For private anonymous mappings, MADV_DONTNEED establishes zero-fill-on-demand semantics for subsequent accesses after the operation succeeds. MADV_FREE instead allows old contents to survive temporarily and delegates the discard time to the kernel.
That distinction affects software that treats memory contents as state. With MADV_DONTNEED, the caller has crossed an immediate content boundary. With MADV_FREE, the caller has declared the prior content disposable but cannot infer whether reclaim has happened from the return of madvise() alone.
Neither operation is equivalent to releasing the virtual address range. The mapping itself persists, and later accesses remain valid subject to its normal protection flags.
LazyFree exposes part of the system state
Linux reports memory marked by MADV_FREE through the LazyFree field in /proc/meminfo. The value describes memory in this reclaimable state at system scope.
grep '^LazyFree:' /proc/meminfoThe field is useful as evidence that pages have entered the lazy-free population, but it is not an ownership ledger for one process and does not provide a synchronization mechanism for detecting reclamation of a specific page.
The contract is about expendable contents
MADV_FREE is appropriate only when the previous bytes are no longer required for correctness. The application retains the address range, and fast reuse can avoid immediate reclamation work, but the kernel is free to discard untouched marked pages later.
The key boundary is therefore content lifetime rather than mapping lifetime. Once the call succeeds, old contents are expendable even if they remain physically present for some time. Any design that still requires those bytes must keep them outside the range passed to MADV_FREE.