get_phys_pages(3)
π get_phys_pages(3) β the library functions manual page, but make it cute π
okay so bestie, this is get_phys_pages(3) from the Library Functions Manual, and it's genuinely so useful and literally nobody talks about it?? we're fixing that rn. π«Ά
β¨ NAME β¨
get_phys_pages, get_avphys_pages β get total and available physical page counts. that's it, that's the whole job, and they ate fr fr. π
π LIBRARY π
Standard C library (libc, -lc). yes queen, it's just libc, no extra drama, no weird dependency, nothing. so lowkey convenient. π
πΈ SYNOPSIS πΈ
#include <sys/sysinfo.h>
long get_phys_pages(void);
long get_avphys_pages(void);
two functions, no arguments, both returning long. it's giving minimalism, it's giving main character energy. β¨
π¦ DESCRIPTION π¦
so get_phys_pages() returns the total number of physical pages of memory available on the system. like, all of it. the whole vibe. the total. π
and get_avphys_pages() returns the number of currently available physical pages of memory on the system. as in right now, in this moment, what's free. not the same number as the other one, bestie, don't mix them up!! π
π RETURN VALUE π
on success, these functions return a nonnegative value exactly as described in DESCRIPTION. (yes, go back up, the DESCRIPTION section is right there, i'll wait. π)
on failure, they return -1 and set errno to indicate the error. classic. no cap. π
π ERRORS π
ENOSYSβ the system could not provide the required information (possibly because the/procfilesystem was not mounted). like, the system literally said "i can't even rn". not mounted/proc= no info =ENOSYS. periodt.
π STANDARDS π
GNU. that's the whole standard section. GNU. iconic, brief, unbothered. π
π HISTORY π
okay so storytime bc this part is actually juicy:
before glibc 2.23, these functions got the required information by scanning the MemTotal and MemFree fields of /proc/meminfo. very hands-on, very DIY, very "i'll just read the file myself". π§
since glibc 2.23, these functions obtain the required information by calling sysinfo(2) instead. so they outsourced it. glow up. β¨
π NOTES π
the following sysconf(3) calls provide a portable means of obtaining the same information as the functions described on this page. so if you want the same tea but portable, here you go bestie:
total_pages = sysconf(_SC_PHYS_PAGES); /* total pages */
avl_pages = sysconf(_SC_AVPHYS_PAGES); /* available pages */
lowkey obsessed with this alternative. note the variable name avl_pages though, not avphys_pages β that's what the manpage says, don't come for me, i'm just the messenger. π«Ά
π EXAMPLES π
the following example shows how get_phys_pages() and get_avphys_pages() can be used. look at this little program, it's so cute, it's so simple:
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
int
main(void)
{
printf("This system has %ld pages of physical memory and "
"%ld pages of physical memory available.\n",
get_phys_pages(), get_avphys_pages());
exit(EXIT_SUCCESS);
}
that's it. that's the whole program. compile it, run it, it tells you your pages. slay. π₯
π SEE ALSO π
yes, only one. it's a small circle. iykyk. π
heads up bestie:ENOSYSis the one to watch for here, and it can pop up if/procisn't mounted β so if a container or chroot is giving you weird results, that might be why. the manpage says "possibly", so don't @ me, i'm quoting fr fr. π
and that's the whole page, bestie!! written by the Linux man-pages crew, version 6.20, dated 2026-02-08 β so when you see get_phys_pages(3) out in the wild, you'll know exactly what's up. go check your page counts, you gorgeous nerd. πβ¨π¦π
π¦ 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: Linux-man-pages-copyleft
- Copyright 2015, William Woodruff <william@tuffbizz.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms