I need to monitor the amount of free physical memory on Linux from within a large C program. The sampling will occur very frequently, so the measurement cannot be performance intensive.
The fact that Linux uses much of the theoretically free memory for cache and buffers means that just measuring the free pages is not sufficient. Using free + cache + buffers gives an overestimate as not all cache/buffers can be freed, but I could get a rough idea of how much generally can't and subtract that from the answer.
Possible options that I've come across so far are:
Parsing /proc/meminfo - but that involves reading from file which is slow.
Extracting the free, cache and buffers values from the output of the Free command - but is there a quick way to do this?
Parsing the /proc/freemem file produced by the API here
- but this is again reading from file. Is there a way to get that output directly?
Are there any other alternative suggestions? Speed is an extremely high priority, and the answer it must accurately represent the amount of memory that my program could expand into (to within a few Mb).