Hi,
The memory used will be only what you keep reference to. In your case, at the end of the loop, both all the patches that your kept in patch_out_max (which I’m not sure how many is that by just reading your code) and all the patches from the last run of the loop (because python does not delete stuff at the end of a loop).