10

From within a Python application, how can I get the total amount of RAM of the system and how much of it is currently free, in a cross-platform way?

Ideally, the amount of free RAM should consider only physical memory that can actually be allocated to the Python process.

3
  • Possible duplicates: stackoverflow.com/questions/466684/… and stackoverflow.com/questions/276052/… Commented Jul 30, 2009 at 4:18
  • 1
    Total RAM and Currently Free aren't cross platform concepts. Windows and GNU/Linux are different operating systems. Which one do you want? The other will be different. Commented Jul 30, 2009 at 13:33
  • psutil does a great job on this one. Commented Jul 18, 2013 at 21:33

5 Answers 5

12

Have you tried SIGAR - System Information Gatherer And Reporter? After install

import os, sigar

sg = sigar.open()
mem = sg.mem()
sg.close() 
print mem.total() / 1024, mem.free() / 1024

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

Although this accepted answer is probably perfectly valid, a better solution in 2015 is now psutil (as recommended by Gringo Suave), which is not part of the standard library, but at least is available totally painlessly via pip.
7

psutil would be another good choice. It also needs a library installed however.

>>> import psutil
>>> psutil.virtual_memory()
vmem(total=8374149120L, available=2081050624L, percent=75.1,
     used=8074080256L, free=300068864L, active=3294920704,
     inactive=1361616896, buffers=529895424L, cached=1251086336)

Comments

6

For the free memory part, there is a function in the wx library:

wx.GetFreeMemory()

Unfortunately, this only works on Windows. Linux and Mac ports either return "-1" or raise a NotImplementedError.

1 Comment

I got NotImplementedError using wx.GetFreeMemory()
3

You can't do this with just the standard Python library, although there might be some third party package that does it. Barring that, you can use the os package to determine which operating system you're on and use that information to acquire the info you want for that system (and encapsulate that into a single cross-platform function).

2 Comments

That's what I figured... but how would I accomplish it on each of the major OSes?
Well, as sunqiang posted, you could try SIGAR (not sure what other dependencies it might have, if any). On linux, you can get memory info from /proc/meminfo, and on other unices you can get it from running vmstat. On windows you'll probably have to call some win32 API (which you can do with ctypes, or with the Python win32 package).
1

In windows I use this method. It's kinda hacky but it works using standard os library:

import os
process = os.popen('wmic memorychip get capacity')
result = process.read()
process.close()
totalMem = 0
for m in result.split("  \r\n")[1:-1]:
    totalMem += int(m)
print totalMem / (1024**3)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.