在 matplotlib savefig 之后 close() 不释放内存 [英] close() not releasing memory after matplotlib savefig

查看:393
本文介绍了在 matplotlib savefig 之后 close() 不释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个 python 程序,它每隔几分钟重新绘制一个图形,但每次运行它使用的内存都会增加一点,很快我使用的树莓派就会变慢.

I'm running a python program which replots a graph every few minutes, but every time it runs the memory it uses goes up a little bit, and soon the raspberry pi I'm using slows to a crawl.

这是相关的一段代码:

import matplotlib.pyplot as plt
import matplotlib.dates as md
from memory_profiler import profile

@profile
def plotter(file_name, plot_name):
    with open(filen_name, 'r') as readings:
        reader = csv.reader(readings, delimiter=',')
        data = [row for row in reader]

    plt.plot(data[2], data[0])
    ax = gca()
    xfmt = md.DateFormatter('%H:%M') # xaxis is datetimes
    ax.xaxis.set_major_formatter(xfmt)
    plt.legend()

    plt.savefig(plot_name, transparent=True)
    plt.clf()
    plt.cla()
    plt.close()

该函数的调用方式如下:

And the function is called with something like:

 while True:
     plotter(file_name, plot_name)
     sleep(100)

memory_profiler 输出不错的输出,但它总是看起来像这样:

The memory_profiler spits out nice output, but it always looks like this:

Line #    Mem usage    Increment   Line Contents
================================================
38     36.2 MiB      0.6 MiB       plt.savefig(plot_name, transparent=True)
39     36.2 MiB      0.0 MiB       plt.clf()
40     36.2 MiB      0.0 MiB       plt.cla()
41     36.2 MiB      0.0 MiB       plt.close()

(该函数的其余部分不会增加内存使用.)
内存在 savefig() 处递增,但尽管我尝试了多种方式关闭图形,但从未释放.有谁知道为什么 close() 不释放内存?

(The rest of the function doesn't increase memory usage.)
The memory is incrementing up at savefig(), but is never released despite the various ways I've tried to close the figure. Anyone know why the close() doesn't release the memory?


我试过另一种方法.


I've tried another method.

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot( some arguments)
fig.savefig(filename)

plt.close('all')
del ax, fig

这也不会减少内存使用量.

This doesn't decrement the memory usage either.


来自其他地方的这个答案(创建一个引用计数的图形) 我尝试了以下方法:

Edit 2:
From this answer elsewhere (Create a figure that is reference counted) I've tried the following:

from matplotlib.backends.backend_svg import FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot etc...
fig.savefig(file_name)
plt.close('all')

这似乎也不起作用.内存仍然单调增加,并且总是在函数的 savefig 行.

This also doesn't seem to work. Memory still increasing monotonically and always at the savefig line of the function.

推荐答案

我遇到了同样的问题.环顾四周后,我找到了一个解决方案:您需要关闭无花果和所有窗口.

I encountered the same issue. After looking around I found a solution: you need to close the fig AND all windows.

import matplotlib.pylab as plt

fig,ax = plt.subplots(1)
plt.plot(X, Y)
fig.savefig(img_name)
fig.clf()
plt.close()

关闭情节和图形后,我的记忆或多或少保持了恒定水平.

After closing both the plot and figure, my memory stayed at a more or less constant level.

您也可以使用以下方法关闭轴:

You can also close the axis using:

ax.cla()

这篇关于在 matplotlib savefig 之后 close() 不释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆