Memory Usage of Strings

Terry Reedy tjreedy at udel.edu
Wed Mar 16 16:38:45 EDT 2011


On 3/16/2011 3:51 PM, Santoso Wijaya wrote:
> ??
>
> Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit
> (AMD64)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import sys
>  >>> L = []
>  >>> for i in xrange(100000):
> ... L.append(str(i) * (1000 / len(str(i))))
> ...
>  >>> sys.getsizeof(L)
> 824464

This is only the size of the list object and does not include the sum of 
sizes of the string objects. With 8-byth pointers, 824464 == 8*100000 + 
(small bit of overhead) + extra space (for list to grow without 
reallocation and copy)

>  >>> L = []
>  >>> for i in xrange(20000):
> ... L.append(str(i) * (5000 / len(str(i))))
> ...
>  >>> sys.getsizeof(L)
> 178024

== 8*20000 + extra

-- 
Terry Jan Reedy




More information about the Python-list mailing list