Memory leak while looping

Brandon Beck bbeck at NOSPAM.austin.rr.com
Thu Apr 17 02:29:29 EDT 2003


It appears your "leak" is with the call to range().  range(1, 10000000)
causes a list of 10000000 to be created in memory.  This has nothing to
do with item being assigned in the body of the loop, and you should see
the same behavior in a script without the for loop and just the call to
range.  Consider using the generator (or maybe it's just generator like)
version of range, xrange.  That should run in constant memory.

Brandon


"Roger Hancock" <roger_hancock at hotmail.com> wrote in message
news:1020cbf8.0304162100.6fc49e6f at posting.google.com...
> I'm just starting to play with writing C extensions for Python.  In
> the process of doing so, I called one of my extension functions
> thousands of times only to discover that a leak was eating up all the
> memory in my system.  Upon further investigation, I found I could
> produce the same result with the following python code with my
> extension completely out of the picture:
>
> #!/usr/bin/env python2
>
> for i in range(1,10000000):
>     item = i
>
>
> I would have thought that the memory for "item" would be freed as new
> "item"s were created.  However, this doesn't seem to be the case.  I
> would appreciate any clarification on this issue.
>
>
> Thanks,
>
> Roger






More information about the Python-list mailing list