maximum length of a list & tuple

Peter Hansen peter at engcorp.com
Sun Apr 11 22:14:33 EDT 2004


Josiah Carlson wrote:

>>> Run the following and the last thing it prints out is your limit...
>>>
>>>    c = 1
>>>    while c < 2**32:
>>>        try:
>>>            d = [1]*c
>>>            print c
>>>            c *= 2
>>>        except:
>>>            break
>>
>>
>>
>> Interesting experiment.  I get 64M on my 384MB machine, which suggests 4
>> bytes per list entry.  Of course, now my swap file is packed full, and 
>> all
>> my apps are running slowly while they page themselves back in...
> 
> 
> Far more than 4 bytes per list entry.  4 bytes to store the integers 
> themselves, but since integers are Python objects, and lists are arrays 
> of pointers to list objects, there is quite a bit of other extra stuff 
> attached.

Actually, unless I'm mistaken that stores only one integer, and a whole
lot of references to it...  which are just pointers (4 bytes each).

The technique is probably flawed in other ways, however, as it
is likely subject to memory fragmentation and other problems.

-Peter



More information about the Python-list mailing list