How to find the total memory used by a Python process?

Sometimes, we want to find the total memory used by a Python process.

In this article, we’ll look at how to find the total memory used by a Python process.

How to find the total memory used by a Python process?

To find the total memory used by a Python process, we can yue the os and psutil modules.

We install the psutil module by running:

pip install psutil

Then, we write:

import os, psutil

process = psutil.Process(os.getpid())
print(process.memory_info().rss)

We use the psutil.Process constructor with the process ID of our script which we get by using os.getpid.

Then we get the memory used with process.memory_info().rss.

Therefore, we should see the number in bytes of the amount of memory used by our script.

Conclusion

To find the total memory used by a Python process, we can yue the os and psutil modules.