[英]Matplotlib doesn't release memory after savefig and close()
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:我尝试使用
memory_profiler
定位内存泄漏,结果如下:
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()
我创建了这样的图:
fig, ax = plt.subplots()
Any suggestion where the 109 - 52 = 57 MiB went?有什么建议 109 - 52 = 57 MiB 去哪儿了?
I am using python 3.3.我正在使用 python 3.3。
# 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希望这可以帮到你
plt.ioff()
在笔记本中为我工作,否则plt.close(fig)
。
# 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!
只需将这些行放在循环的末尾!
Taken from here: Matplotlib errors result in a memory leak.取自此处: Matplotlib 错误导致内存泄漏。 How can I free up that memory?
我怎样才能释放那段记忆?
Which has original ref: https://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html其中有原始参考: 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.也不需要做
plt.close()
或任何事情。 It worked for me.它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.