20

I have a piece of code that works fine looping once or twice but eventually it builds up memory. I tried to locate the memory leakage with memory_profiler and this is the result:

row_nr    Memory_usage    Memory_diff    row_text
 470     52.699 MiB     0.000 MiB      ax.axis('off')
 471     167.504 MiB    114.805 MiB    fig.savefig('figname.png', dpi=600)
 472     167.504 MiB    0.000 MiB      fig.clf()
 473     109.711 MiB    -57.793 MiB    plt.close()
 474     109.711 MiB    0.000 MiB      gc.collect()`

I created the figure like this: fig, ax = plt.subplots()

Any suggestion where the 109 - 52 = 57 MiB went?

I am using python 3.3.

sanyassh
  • 8,100
  • 13
  • 36
  • 70
axel_ande
  • 359
  • 1
  • 4
  • 20
  • Possible duplicate of https://stackoverflow.com/questions/7125710/matplotlib-errors-result-in-a-memory-leak-how-can-i-free-up-that-memory Although that's a workaround, not a clear solution to this problem. – rth Jul 01 '15 at 14:00
  • this question is more generic, probably one of the scenarios there covers your case. If not, let us know more details of use case in question http://stackoverflow.com/questions/7101404/how-can-i-release-memory-after-creating-matplotlib-figures. – J Richard Snape Jul 01 '15 at 20:27
  • Thanks for the try, unfortunately it work, I'm not running anything in django so that didn't help. I can try the multiprocessing but as stated that just avoid the problem and doesn't solve it. I'm using subplots ("fig, ax= plt.subplots()" ) so I can't change from fig = plt.figure() to fig = figure.Figure(), or? Any other suggestions? – axel_ande Aug 10 '15 at 14:58
  • Try `plt.close('all')` at the end of each loop. Works for me with the `agg` backend. – komodovaran_ Jul 25 '18 at 14:42

5 Answers5

15

Nothing posted here worked for me. In my case, it had something to do with running on a server via SSH interpreter. Apparently this will use a non-interactive mode, and that started clearing all memory as normal:

import matplotlib
matplotlib.use('Agg')

Source: https://matplotlib.org/stable/faq/howto_faq.html#work-with-threads

wfgeo
  • 2,716
  • 4
  • 30
  • 51
  • 5
    This answer should be accepted. It is a known bug in matplotlib https://github.com/matplotlib/matplotlib/issues/20300 – Jongsu Liam Kim Dec 06 '21 at 04:24
  • 2
    This reduced the ram usage from GBs to MBs. Thank you! As you said, none of the other posts worked for me. – Alperen Dec 10 '21 at 22:02
  • 3
    In my case, it was not related to SSH, I was running a Python script (saving figures) in my local. – Alperen Dec 11 '21 at 10:53
  • Also running from Python from Eclipse, from 30GB by end of run to a consistent 100MB. Tried everything else to no avail. – twilsonco Jan 31 '22 at 21:49
  • Thanks so much! Not sure if this is a bug or not, but it has been driving me crazy. Setting the backend resolves the issue. No other methods worked. Also it is a local python script in my case, not SSH related – Stefan Shi May 05 '22 at 21:10
10
# Clear the current axes.
plt.cla() 
# Clear the current figure.
plt.clf() 
# Closes all the figure windows.
plt.close('all')

Hope this can help you

YOYO Lu
  • 101
  • 1
  • 4
6

Taken from here: Matplotlib errors result in a memory leak. How can I free up that memory?

Which has original ref: https://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html

To get ax and figure do:

instead of:

import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)

use:

from matplotlib import figure
fig = figure.Figure()
ax = fig.subplots(1)

Also no need to do plt.close() or anything. It worked for me.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
user1953366
  • 1,341
  • 2
  • 17
  • 27
4

plt.ioff() worked for me in notebook, plt.close(fig) otherwise.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
1
# Clear the current axes.
plt.cla() 
# Clear the current figure.
plt.clf() 
# Closes all the figure windows.
plt.close('all')   
plt.close(fig)
gc.collect()

This worked for me. Just put these lines at the end of the loop!

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Sadman Sakib
  • 557
  • 3
  • 10