问题
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