4.2. Profiling Mem

4.2.1. Memray

$ python -m pip install memray
$ python -m memray run myfile.py
$ python -m memray summary memray-orig.py.59092.bin
$ python -m memray tree memray-orig.py.59092.bin
$ python -m memray flamegraph memray-orig.py.59092.bin
$ python -m memray table memray-orig.py.59092.bin
$ python -m memray stats memray-orig.py.59092.bin
../../_images/performance-memray-realtime.png

Figure 4.4. Memray allows for real-time monitoring. [1]

../../_images/performance-memray-pytestplugin.png

Figure 4.5. Memray has pytest plugin. [1]

../../_images/performance-memray.png

Figure 4.6. Memray is Open Source. [1]

4.2.2. Memory Profiler

  • Memory Profiler is a Python module for monitoring memory consumption of a process as well as line-by-line analysis of memory consumption for Python programs.

  • https://github.com/pythonprofilers/memory_profiler

  • https://pypi.org/project/memory-profiler/

  • pip install memory-profiler

  • python -m memory_profiler myfile.py

  • from memory_profiler import profile, memory_usage

  • @profile

  • usage = memory_usage((func, (arg1, arg2), {'kwarg1': 'val'}))

  • python -m mprof run myfile.py

  • python -m mprof plot - requires matplotlib to run

Install:

$ python -m pip install memory-profiler matplotlib

Use Case 1:

Code 4.1. Content of myfile.py
def run():
    [x**x for x in range(0,10_000)]

run()
$ python -m mprof run myfile.py
$ python -m mprof plot

Use Case 2:

Code 4.2. Content of myfile.py
from memory_profiler import profile, memory_usage

@profile
def run():
    [x**x for x in range(0,10_000)]

usage = memory_usage((run, (), {}))
$ python -m memory_profiler myfile.py

4.2.3. References