Why does python seem to allocate more memory than sys.getsizeof accounts?

Example:

import sys

class Test():
    def __init__(self):
        self.a = 'a'
        self.b = 'b'
        self.c = 'c'
        self.d = 'd'
        self.e = 'e'

if __name__ == '__main__':
    test = [Test() for i in range(100000)]
    print(sys.getsizeof(test))

In the Windows task manager: I get a jump of ~ 20 MB when creating a list of 100,000 versus 10.

Using sys.getsizeoff (): for a list of 100000, I get 412,236 bytes; for a list of 10 I get 100 bytes.

This seems extremely disproportionate. Why is this happening?

+5
source share
2 answers

Assigned memory is not disproportionate; you create 100,000 objects! As you can see, they occupy approximately 34 megabytes of space:

>>> sys.getsizeof(Test())+sys.getsizeof(Test().__dict__)
344
>>> (sys.getsizeof(Test())+sys.getsizeof(Test().__dict__)) * 1000000 / 10**6
34.4 #megabytes

You can get a little improvement with __slots__, but you still need about 20 MB of memory to store these 100,000 objects.

>>> sys.getsizeof(Test2())+sys.getsizeof(Test2().__slots__)
200
>>> sys.getsizeof(Test2())+sys.getsizeof(Test2().__slots__) * 1000000 / 10**6
20.0 #megabytes

( mensi answer, sys.getsizeof . , .)

. SO: __slots__? http://docs.python.org/release/2.5.2/ref/slots.html

__slots__:

class Test2():
    __slots__ = ['a','b','c','d','e']

    def __init__(self):
        ...
+2

dict __dict__, 272 . 100 000.

+1

All Articles