python list comprehension shows as memory leak #569
Replies: 1 comment 3 replies
-
What Python version are you using? Can you show what the flame graph shows if you collect native stack traces, using: with memray.Tracker(destination=memray_file_path, trace_python_allocators=True, native_traces=True): I suspect you're seeing the line number cache or instrumentation data cache that the interpreter creates the first time a function is profiled. These caches don't scale with the number of calls to the function or to the size of the list it returns, but rather with the number of byte code instructions in the function, which is why the size is always the same. These are truly leaked by the interpreter, in the sense that they are not freed until after your program has finished running.
This supports my theory - the first time the function is called, the interpreter is creating some structure (like a cache of bytecode offsets to line numbers for the function). That cache is reused across every run of the function, and not destroyed until after the program has finished. |
Beta Was this translation helpful? Give feedback.
-
I was testing how memray will show leaks incase the global variable is changed by allocating memory in some way. This is because I was getting some leak information from libraries. After debugging, I realized it was modifying a global variable. I did not went into actually modifying the global variable yet.
Sample snippet
The result of
memray flamegraph --leaks output.bin
Why is it showing memory leak for list comprehensions ? It seems to leave 440B. Even if I change range to
range(100000)
, it leaves 440B behind. If I call the list comprehension multiple times. It leaves 440B each time.Also If I remove the list comprehension out. It only shows
a = fun()
. Isn't it supposed to show nothing? Because after the function call, the memory is deallocated for that function. If I callfun
twice, it shows the first instance asa = fun()
and not the latter.Beta Was this translation helpful? Give feedback.
All reactions