Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib memory leaks when save figure in a file with qt5 backend #17385

Closed
jlocatelli74 opened this issue May 12, 2020 · 3 comments
Closed

Comments

@jlocatelli74
Copy link

Bug report

Bug summary

When QT5 backend style, there is a memory leaks when saving the figure in a png file. Ther is a simple example with memory output that illustrate the problem.

Code for reproduction

import matplotlib as mpl
mpl.use('Qt5Agg')
import matplotlib.pyplot as plt

import numpy as np

import gc
import sys

for i in range(10000):

    if i % 100 == 0:
        gc.collect()
        print(str(i), len( gc.get_objects()), sys.getsizeof(gc.get_objects()))

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.scatter(np.random.random(10), np.random.random(10))
    fig.savefig('test.png')

    plt.close()

Actual outcome

The is the results with differents backends:

mpl.use('Cairo')

0 43292 361288
100 45464 406496
200 45400 406496
300 45516 406496
400 45460 406496
500 45704 406496
600 45641 406496
700 45701 406496
800 45702 406496
900 45520 406496
1000 45823 406496
1100 45521 406496
1200 45701 406496
1300 45521 406496
1400 45702 406496
1500 45823 406496
1600 45462 406496
1700 45881 406496
1800 45643 406496
1900 45823 406496
2000 45639 406496
2100 45702 406496
2200 45703 406496
2300 45878 406496
2400 45520 406496
2500 45821 406496
2600 45942 406496
2700 45762 406496
2800 45402 406496
2900 45643 406496
3000 45582 406496


mpl.use('Qt5Cairo')

0 55035 457352
100 58230 514568
200 58774 514568
300 58709 514568
400 59008 514568
500 59435 514568
600 59735 514568
700 60273 514568
800 60389 514568
900 60573 514568
1000 60875 514568
1100 61353 514568
1200 61476 514568
1300 62194 514568
1400 62314 514568
1500 62552 514568
1600 62795 514568
1700 63154 514568
1800 63275 514568
1900 64054 514568
2000 64052 514568
2100 64116 514568
2200 64596 578936
2300 65192 578936
2400 64952 578936
2500 65609 578936
2600 65854 578936
2700 66151 578936
2800 66459 578936
2900 66515 578936
3000 67114 578936

mpl.use('Agg')
0 42738 361288
100 45090 361288
200 44908 361288
300 45094 361288
400 44969 361288
500 44849 361288
600 44910 361288
700 44969 361288
800 44910 361288
900 45090 361288
1000 45268 406496
1100 44969 361288
1200 44851 361288
1300 44968 361288
1400 44971 361288
1500 44850 361288
1600 45149 361288
1700 45026 361288
1800 45270 406496
1900 45329 406496
2000 44910 361288
2100 45149 361288
2200 45030 361288
2300 44849 361288
2400 45386 406496
2500 45089 361288
2600 44911 361288
2700 45095 361288
2800 44911 361288
2900 44910 361288
3000 45036 361288

mpl.use('Qt5Agg')

100 57656 514568
200 57954 514568
300 58440 514568
400 58735 514568
500 59095 514568
600 59395 514568
700 59755 514568
800 60055 514568
900 60296 514568
1000 60476 514568
1100 60716 514568
1200 61138 514568
1300 61502 514568
1400 61555 514568
1500 61914 514568
1600 62338 514568
1700 62693 514568
1800 62751 514568
1900 63358 514568
2000 63483 514568
2100 63954 514568
2200 64080 514568
2300 64498 578936
2400 64796 578936
2500 65096 578936
2600 65396 578936
2700 65520 578936
2800 66120 578936
2900 66239 578936
3000 66593 578936

As you can see, when using backend without QT5, there is no memory leaks.

Do you have an idear or solution about this problem?

Thank you for your help.

Expected outcome

Matplotlib version

  • Operating system: Debian 9.12
  • Matplotlib version: 3.0.3
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5agg, agg, Qt5Cairo, cairo
  • Python version: 3.5
  • Jupyter version (if applicable):
  • Other libraries:

python from debian repository
matplotlib from pip

@QuLogic
Copy link
Member

QuLogic commented May 20, 2020

Duplicate of #9141, maybe?

@QuLogic
Copy link
Member

QuLogic commented May 21, 2020

I found a leak from this example, that should be fixed by #17468, but it doesn't have anything to do with Qt5. I'm not sure if there are still any Qt5-related leaks left.

@tacaswell tacaswell added this to the v3.2.2 milestone May 22, 2020
@tacaswell
Copy link
Member

I am going to close this as we have merged #17468 and the leaking code is not on the master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants