How to find free resident memory in Linux using python

Albert Hopkins marduk at letterboxes.org
Sat Oct 2 13:21:22 EDT 2010


On Sat, 2010-10-02 at 07:06 -0700, Sandy wrote:
> Hi all,
> I want to find how much free memory (RAM) is available in my system
> using python. I tried psutil, parsing /proc/meminfo, top output etc
> but not satisfied. For example my gnome-system-monitor gui shows I am
> using 1GB (25%) of my RAM while /proc/meminfo, top, psutil says around
> 2GB is used. Is there anyway I can get the correct availble memory,
> may be adding cache, buffers etc to free memory in these and get some
> value that matches with the gnome-system-monitor gui?
> 
> For example 'top' says:
> Mem:   3995048k total,  2231924k used,  1763124k free,    43480k
> buffers
> 
> while gnome-system-monitor gui shows I am using 1GB (25%)

gnome-system-monitor subtracts buffer/cache usage from total memory
used.  Those numbrs should (nearly match).  For example on one system
g-s-m shows: 356.3 MB used and "free" shows

$ free -m
             total       used       free     shared    buffers    cached
Mem:          3801        782       3018          0         31       395
-/+ buffers/cache:        356       3445
Swap:         4094          0       4094
...

So g-s-m shows the "used" from the second row while "top" is using the
numbers from the first row.

Six of one, half dozen of the other.  It depends on whether you want to
consider memory the kernel allocates to buffers/cache as "used" memory.
All these utilities do read the contents in /proc/meminfo so you can
consider that the "definitive" source of information.







More information about the Python-list mailing list