maximum length of a list & tuple

Peter Hansen peter at engcorp.com
Mon Apr 12 07:18:47 EDT 2004


Josiah Carlson wrote:

>> 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.
> 
> You can't just store the integer.  How would you differentiate between 
> an integer in a list and a pointer?  Answer: you must use PyIntObjects. 
>  Use the source.

Python does not recognize anything called "pointers" at the language
level, only internally.

What I was saying is that the only PyIntObject created was one with
the ob_ival of 1.  Then a list containing one pointer to it was
created.  Then it was replicated "c" times.  Only one integer.

Please refute that claim rather than just arguing a bunch of other
points, none of which I dispute but all of which I claim are irrelevant
to the question at hand.

> Checking the memory usage of Python before and after the creation of 1 
> million integer list, I get a /very/ consistant ~16 bytes per.  That 
> would be 12 bytes for the object, and 4 bytes for each pointer in the 
> list.  

Please post the expression you are using, for comparison.

>  I could go on as to why it doesn't matter in this case (considering the 
> large mallocs necessary for the creation of large lists), but I'll leave 
> that as an exercise to the reader.

Please go on about it.  I'd like to see where you discuss what happens
to the previously memory that was held in earlier lists, as it iterates
through the loop creating exponentially larger lists.

> Again, from the source, 12 bytes for the object, 4 bytes for the pointer 
> in the list, total of 16 bytes per intobject in a list.

Nice.  So each entry in the list is 4 bytes (as I indicated).  And if
they all point to a single instance of the integer 1, how many extra
bytes do you think that allocates?

-Peter



More information about the Python-list mailing list