LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 0
Quick measurement of free system memory from within C program


Hi,

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).

Thanks!
 
Old 07-21-2010, 05:30 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,505

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


Reply

Tags
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


All times are GMT -5. The time now is 03:09 PM.

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