87

How can I get the available and currently used memory from Python? It need to be cross-platform and at least work on at least Windows, Mac OS X and Linux.

I'd like to report the user in my application that there isn't enough memory free to proceed.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Johan Dahlin
  • 25,300
  • 6
  • 40
  • 55
  • Do you need memory or address space? Since at least in Windows, you're not out of memory if you still have swap space - http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx – Michael Stum Jul 23 '12 at 15:28
  • 3
    Looks similar to [this question](http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python), [the psutil library](http://code.google.com/p/psutil/) was recommended – Colin Dunklau Jul 23 '12 at 15:29
  • 3
    You just want `try: ... except MemoryError: ...` – JBernardo Jul 23 '12 at 15:33
  • 6
    @JBernardo catching memory errors is a risky business that usually doesnt work. Afak cPython has NOT been written to handle memory allocation errors gracefully (that'd be a very complex business), and even if you're able to run any code after a `MemoryError`, the state of the interpreter is unreliable and liable to have incorrect info and/or simply fail. – drevicko Mar 31 '15 at 04:31

1 Answers1

142

You should take a look to psutil :

>>> import psutil
>>> psutil.virtual_memory()
svmem(total=16717422592, available=5376126976, percent=67.8, used=10359984128, free=1831890944, active=7191916544, inactive=2325667840, buffers=525037568, cached=4000509952, shared=626225152)
Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • 1
    You have to install the latest psutil (version 0.5.0 from here: https://pypi.python.org/pypi/psutil/0.5.0) to get this to work. Installing from pip installed 4.3.0 for me, which didn't have the phymem_usage() method. For ubuntu, I downloaded the tar.gz file, then did `tar -xvzf psutil-0.5.0.tar.gz`, then `cd psutil-0.5.0`, then `sudo python setup.py install`. You can check your version of psutil by doing `import psutil` and `print(psutil.__version__)` in python – wordsforthewise Aug 16 '16 at 20:49
  • 1
    Using the latest 4.3.0 version, the method is now `psutil.virtual_memory()` I think – wordsforthewise Aug 16 '16 at 21:08
  • This tells you the system memory usage. Is there a way of having similar information but for the current Python process? For example, if I want to do a task until the memory reserved for the current Python process or script reaches 95%. Is there a way of doing this? Maybe I can do that with a combination of your solution and this solution https://stackoverflow.com/a/21632554/3924118? What's your suggestion? But how do I get the percentage of Python reserved to the Python process that has already been used? How do I get the total memory reserved for a Python process? – nbro Sep 26 '19 at 14:31
  • @nbro : ask a new question for this because what you need is interesting ;) (you can link those answers in your question) – Cédric Julien Sep 26 '19 at 15:24