python - 清除通过在 IPython 中绘图分配的内存

标签 python memory matplotlib ipython qtconsole

我正在 IPython QtConsole(和 Notebook)中绘制一些大图。这些占用了很多内存,但是一旦它们被绘制出来,我就不再需要它们了,它们就可以走了。

如何释放内存?

没有以下作品:

close()
clf()
cla()
%reset

释放内存的唯一方法是重新启动内核,我并不总是想这样做(假设我经历了很长的过程才能到达特定点)[准确地说, %reset 确实释放了一些内存,但不如重新启动内核那么多。

如何重现问题:

plot(arange(2e7))

您可能需要更大或更小的数字才能注意到对系统内存的重大影响。

最佳答案

阅读 tcaswell 的评论后,我进一步调查了 IPython 存储的内容。这是我的方法:

## start afresh

## what variables are there before calling plot()?
## make sure you copy() otherwise it's just a reference.
before = vars().copy()

plt.plot(range(10))

## now, what's available after?
after = vars().copy()

## print out the differences
for k in after.keys():
  if k not in before.keys():
    print k

## or a bit more compact
print set(after.keys()) - set(before.keys())

结果是:

before
_i2
_i3
_2

其中 _i* 是输入字符串,_n(n 是一个数字)是输出字符串。 不过,删除这些项目和垃圾收集似乎不会释放内存。

关于python - 清除通过在 IPython 中绘图分配的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27963888/

相关文章:

C++内存分配问题

image - matplotlib imshow():如何制作动画?

python - Pyinstaller 错误 - "setuptools distribution was not found"

python - 如何在 CherryPy 中为 WTForms 集成验证码 (Recaptcha)

c - 发生故障时释放所有分配的内存

python - 叠加来自多个函数调用的 matplotlib 图,同时也单独绘制它们

matplotlib - 无法导入 matplotlib._png (pylab)

python - 如何将矢量场限制在一定半径的圆上?

python - Django 测试。状态 200 和 302

c - 获取 DLL 函数的内存地址