Matplotlib 在 savefig 和 close() 后不释放内存
Matplotlib doesn't release memory after savefig and close()
我有一段代码可以很好地循环一次或两次,但最终它会增加内存。我试图用 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()`
我创建了这样的图:
fig, ax = plt.subplots()
有什么建议 109 - 52 = 57 MiB 去了哪里?
我正在使用 python 3.3.
# Clear the current axes.
plt.cla()
# Clear the current figure.
plt.clf()
# Closes all the figure windows.
plt.close('all')
希望对您有所帮助
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()
这对我有用。只需将这些行放在循环的末尾!
取自此处:Matplotlib errors result in a memory leak. How can I free up that memory?
其中有原始参考:https://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html
要获得斧头和图形,请执行以下操作:
而不是:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)
使用:
from matplotlib import figure
fig = figure.Figure()
ax = fig.subplots(1)
也不需要做 plt.close()
或任何事情。它对我有用。
此处发布的内容对我没有用。就我而言,它与通过 SSH 解释器在服务器上的 运行 有关。显然这将使用非交互模式,并且开始正常清除所有内存:
import matplotlib
matplotlib.use('Agg')
来源:https://matplotlib.org/stable/faq/howto_faq.html#work-with-threads
我有一段代码可以很好地循环一次或两次,但最终它会增加内存。我试图用 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()`
我创建了这样的图:
fig, ax = plt.subplots()
有什么建议 109 - 52 = 57 MiB 去了哪里?
我正在使用 python 3.3.
# Clear the current axes.
plt.cla()
# Clear the current figure.
plt.clf()
# Closes all the figure windows.
plt.close('all')
希望对您有所帮助
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()
这对我有用。只需将这些行放在循环的末尾!
取自此处:Matplotlib errors result in a memory leak. How can I free up that memory?
其中有原始参考:https://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html
要获得斧头和图形,请执行以下操作:
而不是:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)
使用:
from matplotlib import figure
fig = figure.Figure()
ax = fig.subplots(1)
也不需要做 plt.close()
或任何事情。它对我有用。
此处发布的内容对我没有用。就我而言,它与通过 SSH 解释器在服务器上的 运行 有关。显然这将使用非交互模式,并且开始正常清除所有内存:
import matplotlib
matplotlib.use('Agg')
来源:https://matplotlib.org/stable/faq/howto_faq.html#work-with-threads