ipython 清除内存_在IPython中释放巨大的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

gr module rom 'Generate4mRamp.pyc'>

np module ages/numpy/__init__.pyc'>

plt module 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值