在IPython中释放巨大的numpy数组的内存 [英] 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
$ b
变量类型数据/信息
---------------------- --------
gc module< module'gc'(内置)>
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是我的模块,其中包含我用来加载数据立方体的函数。 / p>
---------如何重现错误
以下简单函数能够重现错误。
import numpy as np
import gc
def functionH() :
cube = np.zeros((200,1024,1024))
返回立方体
testcube = functionH()#Run没有任何问题
del testcube
testcube = functionH()#引发内存错误
del testcube
gc.collect()
gc.collect()
testcube = functionH()#仍会引发内存错误
此错误仅在Ipython中发生。在给出 del testcube
之后的简单python(>>>)中,没有内存错误。
你在看价值吗? IPython将输出变量缓存为例如 Out [8]
,所以如果你检查它,它将被保存在内存中。
你可以做%xdel testcube
删除变量并将其从IPython的缓存中删除。或者,%reset out
或%reset array
将清除所有输出历史记录,或仅清除对numpy数组的引用。
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.
When I first start Ipython and call this function, it loads the array into the variable without any problem.
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.
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?
----------------Update
Following is the output of %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.
---------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.
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.
You can do %xdel testcube
to delete the variable and remove it from IPython's cache. Alternatively, %reset out
or %reset array
will clear either all your output history, or only references to numpy arrays.
这篇关于在IPython中释放巨大的numpy数组的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!