系统相关
首页 > 系统相关> > 通过在IPython中绘图分配的清除内存

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

作者:互联网

我在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,memory,matplotlib,ipython,qtconsole
来源: https://codeday.me/bug/20190628/1318874.html