【发布时间】:2016-02-24 20:20:36
【问题描述】:
如果我运行以下代码
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import scipy.misc
import gc
def plot(i):
lena = scipy.misc.lena()
fig = plt.figure(frameon=False,figsize=(2.,2.),dpi=256)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(lena, cmap=plt.cm.gray)
filename = '/tmp/test_{:03d}.png'.format(i)
with open(filename, 'w') as outfile:
fig.canvas.print_png(outfile)
plt.close(fig)
del fig
gc.collect()
for i in range(100):
plot(i)
print(i)
在 Spyder(gentoo linux 上的版本 2.3.5.2 和 python 2.7.10,IPython 3.2.1)中,我看到内存随着迭代次数 i 线性增加,直到最终内存耗尽。 “在 Spyder 中运行”是指标记文本并按 F9。但是,如果我在分离的 IPython 终端中运行它,内存消耗保持不变。
有没有办法在使用 matplotlib 灌封后强制释放 Spyder 中的内存,特别是当输出到文件而不是 Spyder 内部的 IPython 终端时?
【问题讨论】:
-
(Spyder dev here) 如果你在 IPython qtconsole 而不是普通终端中运行这段代码会发生什么?您是否看到同样的内存增加?
-
不,我使用 IPython qtconsole 时没有增加内存。我注意到的一个区别是 Spyder 进程在我的进程列表中显示了“-u”选项,而 IPython 没有。另请注意,如果我在 python 或 IPython 终端中的 Spyder 中运行代码,或者使用 F9 或 F5 运行它,这没有区别。
-
用 Spyder 2.3.8、IPython 4.0.1 和 matplotlib 1.5 再次测试。问题依然存在。
-
我从来没有说过它是固定的 :-)
标签: python matplotlib spyder