memory leak problem with arrays

John Machin sjmachin at lexicon.net
Wed Jun 14 18:47:30 EDT 2006


On 15/06/2006 8:27 AM, sonjaa wrote:
> Serge Orlov wrote:
>> sonjaa wrote:
>>> Hi
>>>
>>> I'm new to programming in python and I hope that this is the problem.
>>>
>>> I've created a cellular automata program in python with the numpy array
>>> extensions. After each cycle/iteration the memory used to examine and
>>> change the array as determined by the transition rules is never freed.
>>> I've tried using "del" on every variable possible, but that hasn't
>>> worked.
>> Python keeps track of number of references to every object if the
>> object has more that one reference by the time you use "del" the object
>> is not freed, only number of references is decremented.
>>
>> Print the number of references for all the objects you think should be
>> freed after each cycle/iteration, if is not equal 2 that means you are
>> holding extra references to those objects. You can get the number of
>> references to any object by calling sys.getrefcount(obj)
> 
> thanks for the info. I used this several variables/objects and
> discovered that little counters i.e. k = k +1 have many references to
> them, up tp 10000+.
> Is there a way to free them?

If (for example) k refers to the integer object 10, all that means is 
that you have 10000+ objects whose value is 10. The references to them 
will be scattered throughout your data structures somewhere.

Caveat: I'm not a numpy user. Now read on:

I would have thought [by extrapolation from the built-in "array" module] 
that numpy would allow you to "declare" a homogeneous array of integers 
which would be internal integers, not python object integers, in which 
case you would not be getting 10000+ references to whatever "k" refers to.

Suggested approaches: read numpy manual, publish relevant parts of your 
code, wait for a numpy guru to appear.

HTH,
John



More information about the Python-list mailing list