views:

410

answers:

3

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  A: 

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

Nick Bastin
That's what I figured... but how would I accomplish it on each of the major OSes?
Algorias
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).
Nick Bastin
+7  A: 

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

sunqiang
+3  A: 

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

wx.GetFreeMemory()
Toni Ruža