I had a similar issue, worked after I put
import gc
and
plt.close('all') gc.collect()
plt.close('all')
gc.collect()
before closing the loop.