9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Algorias
  • 3,043
  • 5
  • 22
  • 16
  • Possible duplicates: http://stackoverflow.com/questions/466684/how-can-i-return-system-information-in-python/ and http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python – John Fouhy Jul 30 '09 at 04: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. – S.Lott Jul 30 '09 at 13:33
  • psutil does a great job on this one. – meawoppl Jul 18 '13 at 21:33

5 Answers5

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

sunqiang
  • 6,422
  • 1
  • 32
  • 32
  • 1
    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. – Peter M Jul 14 '15 at 16:25
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)
Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
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.

Toni Ruža
  • 7,462
  • 2
  • 28
  • 31
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).

Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
  • That's what I figured... but how would I accomplish it on each of the major OSes? – Algorias Jul 30 '09 at 04:06
  • 1
    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 Jul 30 '09 at 04:51
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)