Have you tried to identify where in your code the surprising memory allocations are made? You could "bisect search" by adding breakpoints: https://docs.python.org/3/library/functions.html#breakpoint At which point does the problem start manifesting itself?