Why I fail so bad to check for memory leak with this code?

MRAB python at mrabarnett.plus.com
Thu Jul 21 16:23:25 EDT 2022


On 21/07/2022 20:47, Marco Sulla wrote:
> I tried to check for memory leaks in a bunch of functions of mine using a
> simple decorator. It works, but it fails with this code, returning a random
> count_diff at every run. Why?
> 
> import tracemalloc
> import gc
> import functools
> from uuid import uuid4
> import pickle
> 
> def getUuid():
>      return str(uuid4())
> 
> def trace(func):
>      @functools.wraps(func)
>      def inner():
>          tracemalloc.start()
> 
>          snapshot1 = tracemalloc.take_snapshot().filter_traces(
>              (tracemalloc.Filter(True, __file__), )
>          )
> 
>          for i in range(100):
>              func()
> 
>          gc.collect()
> 
>          snapshot2 = tracemalloc.take_snapshot().filter_traces(
>              (tracemalloc.Filter(True, __file__), )
>          )
> 
>          top_stats = snapshot2.compare_to(snapshot1, 'lineno')
>          tracemalloc.stop()
> 
>          for stat in top_stats:
>              if stat.count_diff > 3:
>                  raise ValueError(f"count_diff: {stat.count_diff}")
> 
>      return inner
> 
> dict_1 = {getUuid(): i for i in range(1000)}
> 
> @trace
> def func_76():
>      pickle.dumps(iter(dict_1))
> 
> func_76()

It's something to do with pickling iterators because it still occurs 
when I reduce func_76 to:

@trace
def func_76():
     pickle.dumps(iter([]))


More information about the Python-list mailing list