Skip to content

Fixes a memory leak where each spines' path was a view of the spine path #89

Merged
merged 2 commits into from Apr 25, 2011

1 participant

@mdboom
Matplotlib Developers member
mdboom commented Apr 21, 2011

Fixes a memory leak where each spines' path was a view of the spine path of the previous run.

The following code (simplified to its essence from an example by Caleb Constantine) leaks memory very slowly -- detectable only using valgrind. It turns out that if the axes are cleared and reused rather than deleted, each spine's path is created as a view based on the spine's path in the previous draw. This creates a long reference chain rather than freeing the previous draw's path view.

This pull request fixes the leak, but @astraw may want to have a look at the fix... I'm not entirely sure why the copy is needed at all -- it hasn't actually been creating a copy all this time any way. Perhaps an alternative solution is just to remove the copy and the assignment (marked with the comment "# replace") altogether?

import numpy
import matplotlib
matplotlib.use("Agg")
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

figure = Figure(dpi=None)
canvas = FigureCanvas(figure)
axes = figure.add_subplot(1,1,1)

def draw(channel, seconds):
    axes.clear()
    axes.plot(channel, seconds)
    canvas.print_figure('test.png')

channel = numpy.sin(numpy.arange(1000))
seconds = numpy.arange(len(channel))

for i in range(10):
    draw(channel, seconds)
@mdboom mdboom added a commit that referenced this pull request Apr 25, 2011
@mdboom mdboom Merged pull request #89 from mdboom/spines_memory_leak.
Fixes a memory leak where each spines' path was a view of the spine path
0ce57c3
@mdboom mdboom merged commit 0ce57c3 into matplotlib:v1.0.x Apr 25, 2011
@richbwood richbwood referenced this pull request Dec 19, 2012
Closed

test_pcolormesh hangs #1609

@mdboom mdboom deleted the mdboom:spines_memory_leak branch Mar 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.