How to get peak memory usage of python script?
Asked Answered
T

3

16

I'm doing some extensive scientific python calculations and whant to know execution time and memory footprint of python script.

So how to get peak memory usage of python script?

If it matters I'm on Windows and use python 2.7.

Two answered 30/10, 2015 at 7:40 Comment(0)
I
7

The resource module can give you this. Works in both Python 2 and Python 3.

import resource
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

This is peak memory in kilobytes. The user and system time is also included in the value from getrusage.

Inhuman answered 20/2, 2022 at 1:16 Comment(2)
This is only available in Unix - in the Python Standard Library documentation, it's listed under "Unix Specific Services": docs.python.org/3.10/library/index.htmlCalamus
Note that this is only in units of kilobytes on Linux and BSD. On OSX, it is in units of bytes.Autobiographical
T
4

For the peak memory, as you are on Windows, you can use psutil and psutil.Process.memory_info, for example to get the peak working set size, in bytes:

>>> import psutil
>>> p = psutil.Process()
>>> p.memory_info().peak_wset
238530560L

As per the link above, you can get more details about some Windows specific fields on this page.

Tidy answered 14/12, 2016 at 13:58 Comment(1)
And what about Linux?Hothouse
G
2

Sounds like you are looking for a memory profiler. Memory_profiler is one that you can dive into which line is giving you the problems and with some querying you can figure out which area is the biggest in memory consumption.

https://pypi.python.org/pypi/memory_profiler and since you are using windows it will also need this https://pypi.python.org/pypi/psutil

Good Luck!

Geronimo answered 30/10, 2015 at 8:52 Comment(1)
I don't think this is what the question asked, the question was about to get the peak memory.Tidy

© 2022 - 2024 — McMap. All rights reserved.