LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04: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, 04:30 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

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



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 08:38 PM
how to get free memory of the unix system hchoonbeng Linux - Newbie 4 10-08-2008 09:43 PM
Total system power measurement piyush masrani Linux - General 2 07-09-2008 11:43 PM
Free Memory Not Reclaimed By System Sheado Programming 3 09-28-2007 03:38 PM
What network measurement program good to use ?? wahaha Linux - Software 3 03-16-2007 10:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 AM.

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
Open Source Consulting | Domain Registration