Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-21-2010, 05:10 AM   #1
LQ Newbie
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
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).

Old 07-21-2010, 05:30 AM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,671

Rep: Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935Reputation: 3935
/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.


free, memory

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
view memory of a quick running program youwin Linux - General 3 02-26-2009 09:38 PM
how to get free memory of the unix system hchoonbeng Linux - Newbie 4 10-08-2008 10:43 PM
Total system power measurement piyush masrani Linux - General 2 07-10-2008 12:43 AM
Free Memory Not Reclaimed By System Sheado Programming 3 09-28-2007 04:38 PM
What network measurement program good to use ?? wahaha Linux - Software 3 03-16-2007 11:52 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration