在 IPython 中释放巨大的 numpy 数组的内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16261240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Releasing memory of huge numpy array in IPython
提问by indiajoe
UPDATE:- This problem solved itself after a machine reboot. Not yet able to figure out why this error was happening before.
更新:-此问题在机器重新启动后自行解决。尚无法弄清楚之前发生此错误的原因。
I have a function that loads a huge numpy array (~ 980MB) and returns it.
我有一个函数可以加载一个巨大的 numpy 数组(~ 980MB)并返回它。
When I first start Ipython and call this function, it loads the array into the variable without any problem.
当我第一次启动 Ipython 并调用这个函数时,它将数组加载到变量中,没有任何问题。
But if I run the same command again, it exits raising a "Memory Error".
但是如果我再次运行相同的命令,它会退出引发“内存错误”。
I tried the following,
我尝试了以下,
del hugeArray
Still the same error was occurring. I even tried the following
仍然发生同样的错误。我什至尝试了以下
del hugeArray
gc.collect()
gc.collect()
Initially, gc.collect()returned 145 and the second call returned 48.
But even after this when I call the function, it was still raising a Memory error.
最初,gc.collect()返回 145,第二次调用返回 48。但即使在此之后,当我调用该函数时,它仍然引发内存错误。
The only way I could load again was to restart ipython. Is there something I can do to free all memory in ipython, so that I don't have to restart it?
我可以再次加载的唯一方法是重新启动 ipython。我可以做些什么来释放 ipython 中的所有内存,这样我就不必重新启动它了吗?
----------------Update
- - - - - - - - 更新
Following is the output of %whos
以下是输出 %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'>
Out of this, gr is my module containing the function which i used to load the data cube.
其中,gr 是我的模块,包含我用来加载数据立方体的函数。
---------How to Reproduce the error
---------如何重现错误
The following simple function is able to reproduce the error.
下面的简单函数能够重现错误。
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
This error is occurring only in Ipython. In simple python (>>>) after giving del testcube, there is no Memory Error.
此错误仅发生在 Ipython 中。在给出之后的简单python(>>>)中del testcube,没有内存错误。
回答by Thomas K
Are you looking at the value? IPython caches output variables as e.g. Out[8], so if you examine it, it will be kept in memory.
你在看价值吗?IPython 将输出变量缓存为 eg Out[8],因此如果您检查它,它将保存在内存中。
You can do %xdel testcubeto delete the variable and remove it from IPython's cache. Alternatively, %reset outor %reset arraywill clear either all your output history, or only references to numpy arrays.
您可以%xdel testcube删除变量并将其从 IPython 的缓存中删除。或者,%reset out或%reset array将清除所有输出历史记录,或仅对 numpy 数组的引用。

