[IPython-dev] memory leak?

Jörgen Stenarson jorgen.stenarson at bostream.nu
Fri Apr 7 13:45:28 EDT 2006


Fernando Perez skrev:
> Jörgen Stenarson wrote:
>> hi,
>>
>> lately I have been using ipython to %run scripts that create large 
>> numeric arrays (in my case say 1000x1000 complex numbers). The problem 
>> I see is that when I rerun the script its like all variables created 
>> in that file are not freed even though if they have not been accessed 
>> in a way that would put them in the In or Out cache. For instance 
>> %running a file containing
>>
>> import Numeric
>> a=Numeric.zeros((1000,1000),"D")
>>
>> repeatedly without any intervening commands will have memory usage go 
>> up with every invocation of %run. But using execfile from an ordinary 
>> python prompt on the same file this is not the case. Is this a known 
>> problem (I'm on windows)? If it is, is there some way to manually free 
>> this memory?
> 
> I suspect the difference comes from the fact that ipython  updates the 
> interactive namespace with the namespace where execfile() ran, so you do 
> end up holding a reference to 'a' in your user namespace.  However, on 
> each run this reference gets updated, so at least all previous instances 
> should be freed.
> 
> IPython can't strictly speaking _leak_ memory in the C sense, given that 
> we have no extension code of our own.  What can happen is that we hold 
> references to stuff which prevent garbage collection from doing its 
> job.  Just a technicality (since the end effect is the same, lost 
> memory), but I wanted to clarify the language.
> 
> Try running a garbage collection manually,  that may help:
> 
> import gc
> gc.collect()
> 
> and let us know what you see.
> 
> Cheers,
> 
> f
> 

Hi,

The gc.collect() works as long as I have not done something that will 
put results in Out. I have tried Out.clear() and deleting all _x where x 
is a number. But that doesn't do the trick, is there some other way of 
clearing the output cache?

/Jörgen




More information about the IPython-dev mailing list