pythonprofilers / memory_profiler

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

Unexpected result from example.py #259

Open kspieks opened 5 years ago

kspieks commented 5 years ago

In contrast to some other open issues, Mem usage and Increment seem consistent for me. However, I tried 3 methods of deleting b (del b, assigning b to None, and using the garbage collector) and none of them freed the memory. Is mprof not correctly tracking the removal of b? If the memory is not actually being released, could someone help me understand why? I get the same result when running mprof run example.py and python example.py

Line # Mem usage Increment Line Contents

 3     35.0 MiB     35.0 MiB   @profile
 4                             def my_func():
 5     42.7 MiB      7.7 MiB       a = [1] * (10 ** 6)
 6    195.3 MiB    152.6 MiB       b = [2] * (2 * 10 ** 7)
 7    195.3 MiB      0.0 MiB       del b
 8    195.3 MiB      0.0 MiB       b = None
 9    195.3 MiB      0.0 MiB       gc.collect()
10    195.3 MiB      0.0 MiB       return a 
kspieks commented 5 years ago

As an update, the above issue may have been caused by using the default sampling rate of 0.1 seconds. When running this again with -T 0.001, I get the output below which decreases the memory usage but doesn't show the corresponding decrement, which is mentioned in issue #253 as well. Is it correct that the sampling rate is the only thing to consider here?

Line # Mem usage Increment Line Contents

 3     37.3 MiB     37.3 MiB   @profile
 4                             def my_func():
 5     45.0 MiB      7.6 MiB       a = [1] * (10 ** 6)
 6    197.5 MiB    152.6 MiB       b = [2] * (2 * 10 ** 7)
 7     45.0 MiB      0.0 MiB       del b
 8     45.0 MiB      0.0 MiB       b = None
 9     45.0 MiB      0.0 MiB       gc.collect()
10     45.0 MiB      0.0 MiB       return a
FyZzyss commented 1 year ago

Same with macOS 12.0.1 and python 3.11.2