Clear memory allocated by plotting in IPython

我的梦境 提交于 2020-01-02 05:42:05

问题


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.

How can I free up memory?

None of the following works:

close()
clf()
cla()
%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].

How to replicate the problem:

plot(arange(2e7))

You may need a bigger or a smaller number to notice a big impact on your system memory.


回答1:


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())

Result is:

before
_i2
_i3
_2

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.



来源:https://stackoverflow.com/questions/27963888/clear-memory-allocated-by-plotting-in-ipython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!