Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script knows how much memory it's using [duplicate]

How can a python script know the amount of system memory it's currently using? (assuming a unix-based OS)

like image 961
ʞɔıu Avatar asked Aug 06 '09 18:08

ʞɔıu


People also ask

How much memory does a Python process use?

We can get this information using the handy psutil library, checking the resident memory of the current process: With this particular measurement, we’re using 3083MB, or 3.08GB, and the difference from the array size is no doubt the memory used by the Python interpreter and the libraries we’ve imported.

How do I check memory usage in Python?

Memory Profiler The Memory Profiler is a python package that evaluates each line of Python code written within a function and correspondingly checks the usage of internal memory. We can either use pip or conda package managers to install this package.

What is the unit of peak memory usage in Python?

>>> resource.getrusage (resource.RUSAGE_SELF).ru_maxrss 2656 # peak memory usage (kilobytes on Linux, bytes on OS X) The Python docs don't make note of the units. Refer to your specific system's man getrusage.2 page to check the unit for the value. On Ubuntu 18.04, the unit is noted as kilobytes. On Mac OS X, it's bytes.

Is your Python batch process using too much memory?

Your Python batch process is using too much memory, and you have no idea which part of your code is responsible. You need a tool that will tell you exactly where to focus your optimization efforts, a tool designed for data scientists and scientists.


1 Answers

If you want to know the total memory that the interpreter uses, on Linux, read /proc/self/statm.

If you want to find out how much memory your objects use, use Pympler.

like image 190
Martin v. Löwis Avatar answered Sep 29 '22 11:09

Martin v. Löwis