8000 large memory leak in new contour routine · Issue #6940 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
large memory leak in new contour routine #6940
Closed
@efiring

Description

@efiring

In master and in 1.5.1, the new C++ contour routine has a massive memory leak; the 'legacy' mode does not. The following script illustrates this; run it as-is and then with the 'legacy' kwarg uncommented for comparison. (The script is long only because it seems report_memory has been removed from cbook. Unless it has simply been moved elsewhere or renamed, we need to bring it back.)

import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
# from matplotlib.cbook import report_memory

import os, sys

def report_memory(i=0):  # argument may go away
    'return the memory consumed by process'
    from matplotlib.compat.subprocess import Popen, PIPE
    pid = os.getpid()
    if sys.platform == 'sunos5':
        try:
            a2 = Popen('ps -p %d -o osz' % pid, shell=True,
                       stdout=PIPE).stdout.readlines()
        except OSError:
            raise NotImplementedError(
                "report_memory works on Sun OS only if "
                "the 'ps' program is found")
        mem = int(a2[-1].strip())
    elif sys.platform.startswith('linux'):
        try:
            a2 = Popen('ps -p %d -o rss,sz' % pid, shell=True,
                       stdout=PIPE).stdout.readlines()
        except OSError:
            raise NotImplementedError(
                "report_memory works on Linux only if "
                "the 'ps' program is found")
        mem = int(a2[1].split()[1])
    elif sys.platform.startswith('darwin'):
        try:
            a2 = Popen('ps -p %d -o rss,vsz' % pid, shell=True,
                       stdout=PIPE).stdout.readlines()
        except OSError:
            raise NotImplementedError(
                "report_memory works on Mac OS only if "
                "the 'ps' program is found")
        mem = int(a2[1].split()[0])
    elif sys.platform.startswith('win'):
        try:
            a2 = Popen(["tasklist", "/nh", "/fi", "pid eq %d" % pid],
                       stdout=PIPE).stdout.read()
        except OSError:
            raise NotImplementedError(
                "report_memory works on Windows only if "
                "the 'tasklist' program is found")
        mem = int(a2.strip().split()[-2].replace(',', ''))
    else:
        raise NotImplementedError(
            "We don't have a memory monitor for %s" % sys.platform)
    return mem


np.random.seed(0)
z = np.random.randn(500, 100)

for i in range(200):
    fig, ax = plt.subplots()
    ax.contourf(z)  # , corner_mask='legacy')
    fig.savefig('temp.png')
    plt.close(fig)
    if i % 10 == 0:
        print(i, report_memory(i))

Metadata

Metadata

Assignees

Labels

Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.status: confirmed bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0