How to find free resident memory in Linux using python

S

Sandy

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


Thanks in advance,
dksr
 
A

Albert Hopkins

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

Seebs

I want to find how much free memory (RAM) is available in my system
using python.

The question is essentially incoherent on modern systems. You'd have to
define terms. Consider that on a given system, it's quite possible that
gigabytes of space are being used to cache disk buffers. Some of those
could be freed as soon as they get written; some could be freed simply by
dropping them, since they're just a read cache. There may be data which
have been paged out, and will be paged back in as soon as possible -- meaning
that freeing up space wouldn't actually increase the number of available
pages of memory for long.

So basically, the question isn't all that well defined. You can get all
sorts of numbers. They may or may not mean anything.

-s
 
S

Sandy

The question is essentially incoherent on modern systems.  You'd have to
define terms.  Consider that on a given system, it's quite possible that
gigabytes of space are being used to cache disk buffers.  Some of those
could be freed as soon as they get written; some could be freed simply by
dropping them, since they're just a read cache.  There may be data which
have been paged out, and will be paged back in as soon as possible -- meaning
that freeing up space wouldn't actually increase the number of available
pages of memory for long.

So basically, the question isn't all that well defined.  You can get all
sorts of numbers.  They may or may not mean anything.

-s

Thanks all for your suggestions.
 
L

Lawrence D'Oliveiro

The question is essentially incoherent on modern systems.

And then there’s the fact that, on most Linux systems, by default you never
get a failure trying to ask for more RAM. Instead, if the system runs short,
it wakes up the dreaded OOM-killer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top