How to clear memory completely of all matplotlib plots
Asked Answered
S

7

47

I have a data analysis module that contains functions which call on the matplotlib.pyplot API multiple times to generate up to 30 figures in each run. These figures get immediately written to disk after they are generated, and so I need to clear them from memory.

Currently, at the end of each of my function, I do:

import matplotlib.pyplot as plt

plt.clf()

However, I'm not too sure if this statement can actually clear the memory. I'm especially concerned since I see that each time I run my module for debugging, my free memory space keeps decreasing.

What do I need to do to really clear my memory each time after I have written the plots to disk?

Submergible answered 27/2, 2015 at 4:7 Comment(6)
Have you tried plt.close("all")? I have a script that creates about 60 charts with 3 axes each. I use this at the end of each iteration. Memory consumption seems normal enough.Mungovan
Thanks for your answer! It worked! And I also found this great stackoverflow post #8214022 that has the answer to my question!Submergible
The post #8214022 does not include plt.close("all"). I tried using all the suggested options in that post, but only plt.close("all") solved a problem I had myself.Triley
Possible duplicate of When to use cla(), clf() or close() for clearing a plot in matplotlib?Maxine
Does this answer your question? How can I release memory after creating matplotlib figuresHandhold
This one uses multiproc to avoid the problem: matplotlib-errors-result-in-a-memory-leak-how-can-i-free-up-that-memoryGenealogy
A
38

After one week trials, I got my solution! Hope it can help you. My demo is attached.

import matplotlib.pyplot as plt
import numpy as np

A = np.arange(1,5)
B = A**2

cnt=0
while(1):  
    cnt = cnt+1
    print("########### test %d ###########" % cnt)

    # here is the trick: 
    # set the figure a 'num' to prevent from re-malloc of a figure in the next loop 
    # and set "clear=True" to make the figure clear
    # I never use plt.close() to kill the figure, because I found it doesn't work.
    # Only one figure is allocated, which can be self-released when the program quits.
    # Before: 6000 times calling of plt.figure() ~ about 1.6GB of memory leak
    # Now: the memory keeps in a stable level
    fig = plt.figure(num=1, clear=True)
    ax = fig.add_subplot()

    # alternatively use an other function in one line
    # fig, ax = plt.subplots(num=1,clear=True)

    ax.plot(A,B)
    ax.plot(B,A)

    # Here add the functions you need 
    # plt.show()
    fig.savefig('%d.png' % cnt)
Acropetal answered 26/1, 2021 at 23:3 Comment(7)
Can testify, works like a charm. Beware, if you use plt.close(f) in combination with the above solution, the memory leak still occurs.Stound
Perfect solution! In my end, I added fig.clear() and plt.close(fig) after saving figures to completely clean the RAM.Marocain
THANK YOU. Agreed that using plt.close() is the WRONG way to go. THANK YOU!Supplementary
Completely solved my issue after many confusing attempts - amazing!Syncretism
num=1, clear=True worked perfectly. You are a life saver. I had scripts creating dozens of charts for hundreds of geographies, and it was ballooning to 100gb in memory. fig.clear and plt.close were not working for me, but after this the script runs along at only 1.5 gigs of ram, which is essentially just the actual dataset held in memory.Septimal
Could you not just assign the num to be unixtime and that way the figure's ID is always going to be unique without having to worry about it?Spracklen
Don't appear to be able to assign a figsize=(x,y) with this num/clear trick.Spracklen
B
33

Especially when you are running multiple processes or threads, it is much better to define your figure variable and work with it directly:

from matplotlib import pyplot as plt

f = plt.figure()
f.clear()
plt.close(f)

In any case, you must combine the use of plt.clear() and plt.close()

UPDATE (2021/01/21)

If you are using a MacOS system along with its default backend (referred as 'MacOSX'), this does NOT work (at least in Big Sur). The only solution I have found is to switch to other of the well-known backends, such as TkAgg, Cairo, etc. To do it, just type:

import matplotlib
matplotlib.use('TkAgg') # Your favorite interactive or non-interactive backend
Beggarly answered 24/4, 2019 at 16:40 Comment(1)
i was able get around this problem with a mac by forcing the garbage collector with the gc module: gc.collect(generation=2) after closing, clearing, and flagging with delErnie
K
6

I have data analysis module that contains functions which call on Matplotlib pyplot API multiple

Can you edit your functions which is calling matplotlib? I was facing the same issue, I tried following command but none of it worked.

plt.close(fig)
fig.clf()
gc.collect()
%reset_selective -f fig

Then one trick worked for me, instead of creating a new figure every time, I pass the same fig object to the function and this solved my issue.

for example use,

fig = plt.figure()
for i in range(100):
    plt.plot(x,y)

instead of,

for i in range(100):
    fig = plt.figure()
    plt.plot(x,y)
Krill answered 1/6, 2019 at 22:9 Comment(0)
G
5

This is more of a test suite than an answer to the question. Here I show that as of Dec 2021, no provided solution actually clears the memory.

We need the following libraries:

import os
import psutil 
import numpy
import matplotlib
import matplotlib.pyplot

I create a single function which should clear ALL matplotlib memory:

def MatplotlibClearMemory():
    #usedbackend = matplotlib.get_backend()
    #matplotlib.use('Cairo')
    allfignums = matplotlib.pyplot.get_fignums()
    for i in allfignums:
        fig = matplotlib.pyplot.figure(i)
        fig.clear()
        matplotlib.pyplot.close( fig )
    #matplotlib.use(usedbackend) 

I make a script that creates 100 figures then attempts to delete them all from memory:

#Use TkAgg backend because it works better for some reason:
matplotlib.use('TkAgg')

#Create fake data for our figures:
x = numpy.arange(1000)

#Get system process information for printing memory usage:
process = psutil.Process(os.getpid())

#Check memory usage before we create figures:
print('BeforeFigures: ', process.memory_info().rss)  # in bytes

#Make 100 figures, and check memory usage:
for n in range(100):
    matplotlib.pyplot.figure()
    matplotlib.pyplot.plot(x, x)
print('AfterFigures:  ', process.memory_info().rss)  # in bytes

#Clear the all the figures and check memory usage:
MatplotlibClearMemory( )
print('AfterDeletion: ', process.memory_info().rss)  # in bytes

Which outputs memory remaining:

>>> BeforeFigures: 76083200
>>> AfterFigures:  556888064
>>> AfterDeletion: 335499264

Less than half the memory allocated is cleared (much less if using standard back-end). The only working solutions on this stack overflow page avoid placing multiple figures in memory simultaneously.

Genealogy answered 18/9, 2021 at 0:27 Comment(1)
This seems to asymptotically approach clearing half the memory as I create more figures, or make them hold more data.Genealogy
D
1

There is unfortunaltely no solution:

See: https://github.com/matplotlib/matplotlib/issues/20300

This only happens if you work in a GUI backend, create new figures, but don't show them. This way of usage is not reasonable. There are working usage patterns for all relevant scenarios.

Doleful answered 27/3, 2023 at 19:1 Comment(3)
This should be the accepted answer! Not only is there no solution, there is agreement in the matplotlib development community to never fix the problem.Genealogy
"[Issue closed] as 'won't fix' because the memory leak doesn't happen for proper usage"Genealogy
You can try plt.switch_backend('agg')Ling
A
0

I use plt.close("all") and it works as expected.

Anklet answered 6/9, 2023 at 11:47 Comment(0)
P
-1

Late answer, but this worked for me. I had a long sequential code generating many plots, and it would always end up eating all the RAM by the end of the process. Rather than calling fig.close() after each figure is complete, I have simply redefined the plt.figure function as follows, so that it is done automatically:

import matplotlib.pyplot as plt
import copy
try:
   # if script it run multiple times, only redefine once
   plt.old_figure
except:
  # matplotlib is imported for the first time --> redefine
  plt.old_figure = copy.deepcopy(plt.figure)
  def newfig(*args):
    plt.show()
    plt.close("all")
  return plt.old_figure(*args)
  plt.figure = newfig

I am well aware that this is not a nice solution, however it easy, quick, and did the trick for me ! Maybe there's a way to decorate plt.figure instead of redefining it.

Presto answered 18/6, 2020 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.