Python: Memory leak?

sys.getsizeof() is not very useful because it accounts often for only a part of what you expect. In this case, it accounts for the list, but not all integer objects that are in the list. The list takes roughly 4 bytes per item. The integer objects take another 12 bytes each. For example, if you try this:

k = [42] * 9999999
print sys.getsizeof(k)

you'll see that the list still takes 4 bytes per item, i.e. around 40MB, but because all items are pointers to the same integer object 42, the total memory usage is not much more than 40MB.


What is getsizeof()

At first I propose to take a look at what the size-of operator means. You can find the exact description in the documentation. I want to zoom-in on the following sentence.

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

This means that when you ask sys.getsizeof([a]) you don't get the actual size of the array. You only get the size of all memory that is dedicated to managing the list. The list still contains 9999999 integers. Each integer consists of 12 bytes which leads to a total of 114 MB. The sum of the memory dedicated to managing the array 32MB plus the sum of the memory of the data in the array is 146 Mb which comes a lot closer to your result.