通过在IPython中绘图分配清除内存 [英] Clear memory allocated by plotting in IPython
问题描述
我正在IPython QtConsole(和Notebook)中绘制一些大块图。这些占用了很多内存,但是一旦它们被绘制,我就不再需要它们了,它们可以去。
I'm plotting some large plots in IPython QtConsole (and Notebook). These take up a lot of memory, but once they are plotted I don't need them any more and they can go.
如何释放内存?
无以下作品:
close()
clf()
cla()
%reset
唯一释放内存的是重启内核,我并不总是想做(比如我经历了一个漫长的过程来达到一个特定的点)[准确地说,%reset
确实释放了一些内存,但没有重启内核那么多。
The only thing that frees the memory is restarting the kernel, which I don't always want to do (say I worked through a long process to get to a specific point) [To be precise, %reset
does free up some memory, but not as much as restarting the kernel].
如何复制问题:
plot(arange(2e7))
您可能需要更大或更大较小的数字,以注意对您的系统内存的重大影响。
You may need a bigger or a smaller number to notice a big impact on your system memory.
推荐答案
在阅读了tcaswell的评论之后,我对IPython存储的内容进行了更多调查。这是我的方法:
After reading tcaswell's comment, I investigated a bit more what IPython is storing. This is my method:
## 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
数字)是输出的字符串。
删除这些项目和垃圾收集不似乎可以释放内存。
Where _i*
are string of the inputs and _n
(with n
a number) are the strings of the outputs.
Removing those items and garbage collecting does not seem to free up memory though.
这篇关于通过在IPython中绘图分配清除内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!