memory usage

Nagy Gabor linux42 at freemail.c3.hu
Thu May 8 09:01:19 EDT 2003


On 03-May-08 07:35, Skip Montanaro wrote:
>     Nagy> Can you access the list created by range later?
> 
> Sure.  Just assign it to a variable first:
> 
>     ilist = range(1, 600000)
>     for i in ilist:
>         ...

That's clear. But if I don't assign it to any variable, why does it still
occupy space?

> It's complicated by the fact that even though Python deletes objects, the
> underlying malloc() package may not release the memory back to the system
> (for many reasons).

That's OK, as long as python can use the same memory for another object.

> You can't force Python to give memory back to the system.  Any time an
> object's reference count drops to zero Python deletes the object, but
> that doesn't mean the memory will be returned to the operating system.

So probably what happens with range() above is that it allocates the
memory for the list, then since it is not assigned to a variable, it
destroys the list, the memory is not freed, but then a second range() will
use the same space, thus not increasing the memory occupation?
That is good enough for me. Sort of.

What about those integers, floats and interned strings you have mentioned?
If I read 'internable' from a file, assign it to a variable, then this
variable goes out of scope, will this space be usable again?

You wrote, these strings may become immortal on certain versions of
python. Is mine (2.2.1) affected? How do these "immortal" interned strings
behave? What is the purpose of interning them? Will two occurences share
the same memory and the variables will just contain an "intern-pointer"?

> You're better off asking these sorts of questions on comp.lang.python.
> You'll get a broader range of responses, many from people who know a lot
> more about the issues than I do.
Yes, only since you answered to me only, I did not want to post it back to
the list.

Regards,
Gabor





More information about the Python-list mailing list