Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *