pythonprofilers / memory_profiler

Monitor Memory usage of Python code
http://pypi.python.org/pypi/memory_profiler
Other
4.39k stars 380 forks source link

Aggregated (not line based) usage reported #283

Open jackric opened 4 years ago

jackric commented 4 years ago

Discovered this bug while folliowing the Python Data Science Handbook Expecting: sensical line by line increments Getting: a headline memory usage of the function only

Python 3.7.7 (default, Mar 10 2020, 15:43:33)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.15.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %load_ext memory_profiler

In [2]: %%file mprun_demo.py
   ...: def sum_of_lists(N):
   ...:     total = 0
   ...:     for i in range(5):
   ...:         L = [j ^ (j >> i) for j in range(N)]
   ...:         total += sum(L)
   ...:         del L # remove reference to L
   ...:     return total
   ...:
Overwriting mprun_demo.py

In [3]: from mprun_demo import sum_of_lists
   ...: %mprun -f sum_of_lists sum_of_lists(10000)
Filename: /Users/j/data_science/mprun_demo.py

Line #    Mem usage    Increment   Line Contents
================================================
     1     48.0 MiB     48.0 MiB   def sum_of_lists(N):
     2     48.0 MiB      0.0 MiB       total = 0
     3     48.2 MiB      0.0 MiB       for i in range(5):
     4     48.4 MiB      0.1 MiB           L = [j ^ (j >> i) for j in range(N)]
     5     48.4 MiB      0.0 MiB           total += sum(L)
     6     48.2 MiB      0.0 MiB           del L # remove reference to L
     7     48.2 MiB      0.0 MiB       return total

Environment: Homebrew Python 3.7.7 OS: Mac OSX 10.15.4 (Catalina) memory_profiler: 0.57.0