Quick measurement of free system memory from within C program
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).
/proc is *not* your normal filesystem - there is NO physical I/O. Same applies to /sys.
Free reads /proc and does some maths - not much point going there ...
This is a really dodgy quagmire to enter. That code you linked is pretty old - nice idea, but the slab allocator (for example) has changed quite a bit lately. I might have a look at the code if I get some quiet time.
|All times are GMT -5. The time now is 11:25 PM.|