Hi all,

I've come across a memory leak when saving multiple figures in a row.
The figure contains a single pcolormesh artist, which I need to
rasterize in my application. However, turning on rasterization causes
a memory leak.The attached script reproduces the problem; swap out the
commented section at the bottom to eliminate memory growth for each
new figure.

I'm not sure what the cause is, though to toss one idea out, might it
have to do with a references that aren't cleaned up from the
rasterizing part of the mixed-mode renderer?

Tested on Mac OS X and, thanks to Patrick Marsh, on Windows.

Thanks,
Eric
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
from matplotlib.colorbar import ColorbarBase
# from matplotlib.cm import get_cmap

import numpy as np

import gc

def test_rasterized_artist(rasterize_mesh=True):
    """Save an image as a png file"""

    pdfpath = 'test_mplsave.pdf'

    xedge = np.arange(11)-5
    yedge = np.arange(11)-5
    density = np.arange(100).reshape((10,10))

    fig = Figure(figsize=(8,8))
    canvas = FigureCanvasAgg(fig)                                
    fig.set_canvas(canvas)
    ax = fig.add_subplot(1,1,1)

    density_plot  = ax.pcolormesh(xedge,yedge,density)
    density_plot.set_rasterized(rasterize_mesh)
                              
    fig.savefig(pdfpath, dpi=150)

    # The below is not necessary to prevent a leak, but it does make
    # memory usage more compact
    gc.collect()


# This leaks
for i in range(100):
    test_rasterized_artist(rasterize_mesh=True)

# # This doesn't
# for i in range(100):
#     test_rasterized_artist(False)
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to