Checking available memory with sysconf(_SC_AVPHYS_PAGES)

Hi everyone!

I am developing an application and I need to check the ammount of Bytes available. I discovered the sysconf functionality to check some system parameters and variables (such as the virtual memory available for the program).

In order to check the way it works I wrote a code which constantly shows the ammount of MB available (based on the page size of memory). My PC has 16 GB of RAM and the output of the program is 15460 MB at start, but as time goes by it starts to be smaller (15460-15456-15452...)

I would like to know what is the reason of this and if I need to worry (it need a lot of time to reduce but my application should be able to run for huge periods of time) or the OS takes care of it.

Thank you!!
https://www.man7.org/linux/man-pages/man3/sysconf.3.html

These values also exist, but may not be standard.
...
- _SC_AVPHYS_PAGES
The number of currently available pages of physical memory.

There is only a tenuous link between the amount of physical memory and the amount of virtual memory the OS will give to your process.

Your /proc/pid/status file has more useful information about it's own VM usage.
1
2
3
4
5
6
7
8
9
10
11
12
13
$ grep Vm /proc/$(pgrep firefox)/status
VmPeak:	19409416 kB
VmSize:	 3638604 kB
VmLck:	       0 kB
VmPin:	       0 kB
VmHWM:	  423692 kB
VmRSS:	  370380 kB
VmData:	  469208 kB
VmStk:	     132 kB
VmExe:	     604 kB
VmLib:	  134252 kB
VmPTE:	    1972 kB
VmSwap:	       0 kB

https://man7.org/linux/man-pages/man5/proc.5.html for all the gory details.

For your program, if those values remain stable over long periods of time, then you should be fine.

But if they show a gradual upward trend (one not explainable by the work it's doing), you probably have a memory leak.
At some point, the OS will kill your program.
Thanks salem!!

I´ve run the code checking the memory with the function from this link:
https://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-runtime-using-c

It seems to be steady and a more accurate way to check the memory usage.
You could look at the system with htop, sort by "Resident Memory", sit back and watch.
https://htop.dev/
Topic archived. No new replies allowed.