[英]Releasing memory of huge numpy array in IPython
更新: - 这个问题在机器重启后解决了。 还没有弄清楚为什么之前发生这种错误。
我有一个函数加载一个巨大的numpy数组(~980MB)并返回它。
当我第一次启动Ipython并调用此函数时,它会将数组加载到变量中而没有任何问题。
但是,如果我再次运行相同的命令,它将退出提出“内存错误”。
我试过以下,
del hugeArray
仍然发生同样的错误。 我甚至尝试了以下内容
del hugeArray
gc.collect()
gc.collect()
最初, gc.collect()
返回145并且第二次调用返回48.但即使在此之后我调用该函数时,仍然会引发内存错误。
我可以再加载的唯一方法是重启ipython。 我可以做些什么来释放ipython中的所有内存,以便我不必重新启动它?
----------------更新
以下是%whos
的输出
Variable Type Data/Info
------------------------------
gc module <module 'gc' (built-in)>
gr module <module 'Generate4mRamp' <...>rom 'Generate4mRamp.pyc'>
np module <module 'numpy' from '/us<...>ages/numpy/__init__.pyc'>
plt module <module 'matplotlib.pyplo<...>s/matplotlib/pyplot.pyc'>
除此之外,gr是我的模块,包含我用来加载数据立方体的函数。
---------如何重现错误
以下简单函数能够重现错误。
import numpy as np
import gc
def functionH():
cube=np.zeros((200,1024,1024))
return cube
testcube=functionH() #Runs without any issue
del testcube
testcube=functionH() # Raises Memory Error
del testcube
gc.collect()
gc.collect()
testcube=functionH() # Still Raises Memory Error
此错误仅在Ipython中发生。 在给出del testcube
之后的简单python(>>>)中,没有内存错误。
你在看价值吗? IPython将输出变量缓存为例如Out[8]
,因此如果你检查它,它将被保存在内存中。
您可以执行%xdel testcube
删除变量并将其从IPython的缓存中删除。 或者, %reset out
或%reset array
将清除所有输出历史记录,或仅清除对numpy数组的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.