I was doing a foolish thing like:
from itertools import *
rows = combinations(range(0, 1140), 17)
all_rows = []
for row in rows:
all_rows.append(row)
No surprise; I run out of memory address space (32 bit python 3.1)
My question is: how do I calculate how much memory address space I will need for a large list? In this case the list is on the order of 2.3X10^37.
Is there a function in Python that returns the information I am looking for, or actually the size of a smaller but similar list? What are those tools?
There’s a handy function
sys.getsizeof()(since Python 2.6) that helps with this:From that you can see that each integer takes up 12 bytes, and the memory for each reference in a list or tuple is 4 bytes (on a 32-bit machine) plus the overhead (36 or 28 bytes respectively).
If your result has tuples of length 17 with integers, then you’d have
17*(12+4)+28or 300 bytes per tuple. The result itself is a list, so 36 bytes plus 4 per reference. Find out how long the list will be (call it N) and you have36+N*(4+300)as the total bytes required.Edit: There’s one other thing that could significantly affect that result. Python creates new integer objects as required for most integer values, but for small ones (empirically determined to be the range [-5, 256] on Python 2.6.4 on Windows) it pre-creates them all and re-uses them. If a large portion of your values are less than 257 this would significantly reduce the memory consumption. (On Python
257 is not 257+0;-)).