Skip to content

Memory leak when plotting multiple figures with the macOS backend #19769

Closed
@tgrohens

Description

@tgrohens

Bug report

Bug summary

Hi,

I believe I have found a memory issue when plotting multiple figures with the macOS backend.

Here is a minimal working example:

import sys
import matplotlib.pyplot as plt
import numpy as np

import matplotlib
#matplotlib.use('agg')

data = np.random.rand(60, 60)

def plot_expr(plot_name):

    plt.figure(figsize=(9, 8))

    for i in range(60):
        plt.plot(data[i, :])

    plt.savefig(plot_name)

    plt.close('all')


if __name__ == "__main__":

    print(f'python: {sys.version}')
    print(f'matplotlib: {matplotlib.__version__}')
    print(f'backend: {matplotlib.get_backend()}')

    for i in range(50):
        print(f'{i}...')
        plot_expr(f'leak_{i}')

When running the code with memory-profiler, I obtain something like:
leak

Even though I am explicitly calling the plt.close() function.

When using the 'agg' backend instead of the 'MacOSX' backend by uncommenting line 6, I get:
no_leak

Which I would expect to be the normal behavior.

As I have been unable to reproduce this on Linux, I assume that the issue is with the macOS backend.

Matplotlib version

  • Operating system: macOS 11.2.1
  • Matplotlib version 3.3.4
  • Matplotlib backend MacOSX
  • Python version: 3.7.9

I installed Python via venv and matplotlib via pip.

Cheers,

Théotime

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions