1

In a python, how do I get the current interpreter's/process's RAM usage?

To be clear: I'm not looking to get available RAM on the system, or RAM usage of any other process - only RAM usage of the process/interpreter currently doing the python interpretation.

I understand that the number will represent the memory it has allocated and not necessarily the memory (measured in pages) reserved for the process by the OS, resulting in an under-estimation, or that the system may have paged out or compressed some of the memory, but I'm OK with that.

iAdjunct
  • 2,739
  • 1
  • 18
  • 27
  • From python itself, or externally? Because getting the current process usage from the current process is going to be a misleading number – OneCricketeer Dec 17 '17 at 18:07
  • From python itself. I understand that the number will represent the memory it has allocated and not necessarily the memory (measured in pages) reserved for the process by the OS, resulting in an under-estimation, or that the system may have paged out or compressed some of the memory, but I'm OK with that. – iAdjunct Dec 17 '17 at 18:16
  • You could try http://psutil.readthedocs.io/ and get the memory for the same process. On a side note, CPython uses a mem poll, so depending on the memory usage pattern of you application the resident memory may actually be an overestimation. – Augusto Hack Dec 17 '17 at 19:26
  • @AugustoHack - Thank you. Two notes: (1) My hope is that if I evict a bunch of cached lists in response to my resident memory being large that I'll convince the interpreter to deallocate some of its pools; and (2) I'm trying to find a way which doesn't involve third-party libraries (I'm fine with them, but I may also need to use this in an environment where I can't easily use them). The first point in me getting memory usage is to let me know when my assumption of I-have-enough-RAM-for-this becomes invalid - making the program adapt itself is icing on the cake. – iAdjunct Dec 17 '17 at 19:33
  • Is [sys.getallocatedblocks()](https://docs.python.org/3/library/sys.html#sys.getallocatedblocks) helpful? Perhaps you need to look deeper into the [gc](https://docs.python.org/3/library/gc.htm) modules. – import random Dec 17 '17 at 21:50
  • This might answer the RAM usage part: https://stackoverflow.com/questions/938733/total-memory-used-by-python-process – Luc Jan 22 '18 at 21:33
  • Does this answer your question? [How to get current CPU and RAM usage in Python?](https://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python) – wovano Oct 17 '22 at 07:45

0 Answers0