Lists, tuples and memory.

Elbert Lev elbertlev at hotmail.com
Fri Jul 16 15:54:40 CEST 2004


Andrew MacIntyre <andymac at bullseye.apana.org.au> wrote in message news:<mailman.465.1089940443.5135.python-list at python.org>...
> On Fri, 15 Jul 2004, Elbert Lev wrote:
> 
> {...}
> 
> > But then I decieded to compare this pieces:
> >
> > t = tuple(file("D:\\CommonDictionary.txt"))        # 1
> > lstdict = map(lambda x: x.lower().strip(), t)      # 2
> >
> > lstdict = map(lambda x: x.lower().strip(),
> > file("D:\\CommonDictionary.txt")) # 3
> >
> > As expected, after line 2 memory was 5550K, but after line 3 it jumped
> > to 7996K!!!
> >
> > The question:
> >
> > If refference counting is used, why the second assignment to lstdict
> > (line 3) did not free the memory alocated by the first one (line 2)
> > and reuse it?
> >
> > So one more experiment:
> >
> > t = tuple(file("D:\\CommonDictionary.txt"))        # 1
> > lstdict = map(lambda x: x.lower().strip(), t)      # 2
> > del lstdict                                        # 3
> > lstdict = map(lambda x: x.lower().strip(),
> > file("D:\\CommonDictionary.txt")) # 4
> >
> > In this case executing line 4 did not add memory!
> 
> The reason the memory is not released without the explicit del is that the
> reference count of the original object is not adjusted until the
> rebinding of "lstdict" takes place, which is after the new object is fully
> constructed.  The del in your experiment forces this adjustment.

Thanks!

This is what I wanted to know.



More information about the Python-list mailing list