Sometimes, we want to get the total memory used by a Python process.
In this article, we’ll look at how to get the total memory used by a Python process.
How to get the total memory used by a Python process?
To get the total memory used by a Python process, we can use the psutil
module.
To install it, we run
pip install psutil
Then, we write
import os, psutil
process = psutil.Process(os.getpid())
print(process.memory_info().rss)
to create a Process
object with
process = psutil.Process(os.getpid())
We call os.getpid()
to get the process ID of the current script.
And then we call memory_info
to get the amount of memory used in bytes from the rss
property.
Conclusion
To get the total memory used by a Python process, we can use the psutil
module.