Lists, tuples and memory.

Andrew MacIntyre andymac at bullseye.apana.org.au
Fri Jul 16 00:35:39 CEST 2004


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.

--
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac at bullseye.apana.org.au  (pref) | Snail: PO Box 370
        andymac at pcug.org.au             (alt) |        Belconnen  ACT  2616
Web:    http://www.andymac.org/               |        Australia



More information about the Python-list mailing list