Is there a way for a Python program to determine how much memory it's currently using? I've seen discussions about memory usage for a single object, but what I need is total memory usage for the process, so that I can determine when it's necessary to start discarding cached data.
Python – Total memory used by Python process
memory-managementpython
Related Topic
- Python – What are metaclasses in Python
- Python – Does Python have a ternary conditional operator
- Python – How to get the current time in Python
- Python – How to explicitly free memory in Python
- Python – Proper way to declare custom exceptions in modern Python
- Python – Does Python have a string ‘contains’ substring method
- Python – __future__ in Python used for and how/when to use it, and how it works
- C++ – Why is the program slow when looping over exactly 8192 elements
Best Answer
Here is a useful solution that works for various operating systems, including Linux, Windows, etc.:
With Python 2.7 and psutil 5.6.3, the last line should be
instead (there was a change in the API later).
Note:
do
pip install psutil
if it is not installed yethandy one-liner if you quickly want to know how many MB your process takes: