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 /proc filesystem 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: ENOSYS is the one to watch for here, and it can pop up if /proc isn'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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms