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([]))
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to