MemoryError when list append... plz help

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 31 13:57:03 EST 2008


En Wed, 31 Dec 2008 06:34:48 -0200, Steven D'Aprano  
<steve at remove-this-cybersource.com.au> escribió:

> Each time you are appending to the list, you append a tuple:
>
> ((i, j), sim)
>
> where sim is a float and i and j are ints. How much memory does each of
> those take?
>
>>>> sys.getsizeof( ((0, 1), 1.1) )
> 32

(On Windows, 32 bits, I get 36)

> So each entry requires 32 bytes. 60 million times 32 bytes = almost 2GB
> alone. Plus the list itself will require (approximately) between 230MB
> and 460MB just for the pointers.

That was just the size of the "outer" tuple; you have to add the size of  
each element too. First one is another 2-item tuple (36 bytes too) plus  
its elements (two integers, 12 bytes each). Second element is a float and  
takes 16 bytes. Total: 112 bytes per item; the final size may be a bit  
smaller because some objects may be shared (e.g. small integers)

-- 
Gabriel Genellina




More information about the Python-list mailing list