X Tutup
Skip to content

Calling close() on a figure doesn't seem to close it #8560

@mdklatt

Description

@mdklatt

Bug report

Bug summary

Calling pyplot.close() on a figure created with pyplot.subplots() doesn't seem to actually close the figure. After being closed the figure can still be saved (and is not blank), and this seems to cause a memory leak in long-running processes.

Code for reproduction

from resource import getrusage
from resource import RUSAGE_SELF

from matplotlib import use as use_backend
use_backend("Agg")
from matplotlib import pyplot
from numpy.random import randn


def plot():
    """ A simple histogram plot.
    
    """
    fig, axes = pyplot.subplots(1, 1)
    axes.hist(randn(10000))
    pyplot.close(fig)  # should free all resources, doesn't?
    fig.savefig("hist.png")  # should be blank, isn't
    return


if __name__ == "__main__":
    print("before: {:d} kB".format(getrusage(RUSAGE_SELF).ru_maxrss))
    plot()
    print("after: {:d} kB".format(getrusage(RUSAGE_SELF).ru_maxrss))

Actual outcome

image

before: 40920 kB
after: 45632 kB

Expected outcome

The image should be blank because it was saved after the figure was supposedly closed. Ending memory usage should be the same as beginning memory usage because all resources should have been released upon exit from the plot() function.

Matplotlib version

  • Operating System:
    Linux-3.10.0-514.el7.x86_64-x86_64-with-centos-7.3.1611-Core (running as a VirtualBox VM on OS X)

  • Matplotlib Version:
    2.0.0

  • Python Version:
    Python 2.7.5

matplotlib was Installed via pip running in a virtualenv environment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup