Memory leak while looping

Isaac To kkto at csis.hku.hk
Thu Apr 17 01:21:26 EDT 2003


>>>>> "Roger" == Roger Hancock <roger_hancock at hotmail.com> writes:

    Roger> I'm just starting to play with writing C extensions for Python.
    Roger> In the process of doing so, I called one of my extension
    Roger> functions thousands of times only to discover that a leak was
    Roger> eating up all the memory in my system.  Upon further
    Roger> investigation, I found I could produce the same result with the
    Roger> following python code with my extension completely out of the
    Roger> picture:

    Roger> #!/usr/bin/env python2

    Roger> for i in range(1,10000000): item = i


    Roger> I would have thought that the memory for "item" would be freed as
    Roger> new "item"s were created.  However, this doesn't seem to be the
    Roger> case.  I would appreciate any clarification on this issue.

It doesn't seem to me that item is eating up any memory.  When running the
loop (with 10000000 changed to 100000) the memory consumption of python
never goes up.  Of course, the object produced by range(1,100000) would take
up 1M RAM by itself, which is not freed until the loop finishes.  If you
dislike this you might try xrange(1,10000000).

Regards,
Isaac.




More information about the Python-list mailing list