I have faced a matplotlib memory leakage problem while working on my python project that I can't solve. Nothing I found regarding this problem helped. Here is a very simplified script causing aforementioned behaviour:
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
fig = plt.figure()
for i in range(500):
ax = fig.add_subplot(111)
ax.plot([0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5])
plt.savefig(f'test{i:03d}.png')
fig.clear()
This is the memory usage plot I'm getting on various operating systems:
The issue seems to be MacOS related.
Steps to reproduce the plots:
pip install memory-profiler
. I usedmemory-profiler
tool, but any other profiling tool can be used as well.mprof run test.py
mprof plot
Solutions I've tried that didn't help: