posix_madvise(3)
π posix_madvise(3) β like, the manual page bestie π
okay so you're in the Library Functions Manual section, and this is the page for posix_madvise β pronounced however you want, I'm not the police. Let's go π¦
β¨ NAME β¨
posix_madvise β give advice about patterns of memory usage
(yeah it's literally just you telling the kernel your vibes about some memory. that's it. that's the whole job π )
π LIBRARY π
Standard C library (libc, -lc)
πΈ SYNOPSIS πΈ
#include <sys/mman.h>
int posix_madvise(size_t size;
void addr[size], size_t size, int advice);
π Feature Test Macro Requirements for glibc π
(if you wanna know more about this whole situation, go read feature_test_macros(7) β she explains everything)
posix_madvise():_POSIX_C_SOURCE >= 200112L
not gonna lie, forgetting a feature test macro and then getting a cryptic "'posix_madvise' was not declared in this scope" is a canon event for every C girlie π so please, set the macro.
π DESCRIPTION π
So <hotpink>posix_madvise()is the function that lets your app drop a little hint to the system about how it *expects* to use the memory in the address range that starts ataddrand runs forsizebytes. The system is <pink>totally free</pink> to take that advice and use it to make memory accesses faster β or to completely ignore it, whatever, it's her choice. But here's the important part: callingposix_madvise()` shall not affect the semantics of accessing memory in that range. Like, zero behaviour change. It's advisory, bestie. Purely advisory. β¨
The advice argument is one of these:
POSIX_MADV_NORMALβ you've got no special tea to spill about your memory usage patterns for this range. This is the default behaviour, the "no thoughts head empty" option πΈPOSIX_MADV_SEQUENTIALβ you expect to hit this range in order, going from lower addresses up to higher addresses. So the pages here can get aggressively read ahead, and honestly they might get freed pretty soon after they're accessed πPOSIX_MADV_RANDOMβ you expect to access this range randomly, so read ahead is gonna be less useful than it normally would be πPOSIX_MADV_WILLNEEDβ you expect to touch this range in the near future, so read ahead may legit be beneficial πPOSIX_MADV_DONTNEEDβ you expect that you will not be accessing this range in the near future. (note the "expects" β this is a vibe, not a contract)
π«Ά RETURN VALUE π«Ά
- If it slays:
posix_madvise()returns0. π - If it doesn't: it returns a positive error number. (positive, not negative β don't come for me, that's just how she is)
π ERRORS π
EINVALβaddris not a multiple of the system page size, orsizeis negative. yeah this one's on you bestie.EINVALβadviceis invalid. (yes, both of these areEINVAL. yes, you have to figure out which one. keeping it mysterious π )ENOMEMβ addresses in the specified range are partially or completely outside the caller's address space. like, you're pointing at memory that isn't yours. she said no π
π VERSIONS π
POSIX.1 permits an implementation to generate an error if size is 0. On Linux, though, specifying size as 0 is permitted β as a successful no-op. so it just... doesn't do anything, and everyone moves on with their day β¨
In glibc, this function is implemented using madvise(2). BUT β and this is the plot twist β since glibc 2.6, POSIX_MADV_DONTNEED is treated as a no-op, because the corresponding madvise(2) value, MADV_DONTNEED, has destructive semantics. π
so real talk: don't be out here assuming POSIX_MADV_DONTNEED is gonna free anything under glibc. it does nothing. that's the whole point. they protected you from yourself and honestly? iconic of them.
β STANDARDS β
POSIX.1-2008.
π§ HISTORY π§
- glibc 2.2.
- POSIX.1-2001.
(an elder. a legend. a main character since the early 2000s π)
π SEE ALSO π
and that's the page, bestie π one last thing β this whole thing came from Linux man-pages 6.20, dated 2026-02-08, so if you're citing it, that's the receipt. go forth and advise your memory about its usage patterns. periodt π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-2.0-or-later
- Copyright 2015, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms