1

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:

  • MacOS 12.2.1, matplotlib 3.5.1: enter image description here

  • Win 11, matplotlib 3.5.1: enter image description here

The issue seems to be MacOS related.

Steps to reproduce the plots:

  • pip install memory-profiler. I used memory-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:

S. Iakovlev
  • 126
  • 1
  • 8

0 Answers0