Garbage collection working improperly?

Brett C. brett at python.org
Mon Nov 24 14:47:37 EST 2003


Andrew Bennetts <andrew-pythonlist at puzzling.org> wrote in message news:<mailman.973.1069429647.702.python-list at python.org>...
> On Fri, Nov 21, 2003 at 11:58:24AM +0100, Oliver Walczak wrote:
> > Dear List,
> > Trying the following hack:
> > 
> > >>> a = []
> > >>> for i in range(0,9999999):
>  a.append(i)
> > >>> del(a)
> > 
> > Builds up a great list in memory and immediately deletes it. Unfortunately
> > the task manager shows me that i allocated about 155MB in memory before
> > del(), but del only releases about 40MB of them so i'm leaving about 117 MB
> > of reserved memory after deleting the list.
> 
> I believe space allocated for integers is never freed, it is instead added
> to a free-list.  So long as you never have a ridiculous number of integers
> alive simultaneously, it's not a significant problem... 
> 

You're right.  Since an integer is a full-blown object, Python keeps a
list of all created integers alive for maximal reuse.

But there is also no guarantee the object will be collected right
away.  The language spec explicitly states that there is no guarantee
an object will be collected immediately.  If you *have* to collect
immediately then run gc.collect() .

-Brett




More information about the Python-list mailing list