5

What kind of data does the Python interpeter store for an object?
For example, in code like the following:

class MyClass:
    pass

if __name__ == "__main__":
    c = MyClass()
    import sys
    print sys.getsizeof(c),sys.getsizeof(MyClass)

Why is the output 72 and 104? Why is the class bigger than the object instance? What do the class and the object have to store that takes up 72 characters and 104 characters?

Surprisingly, when I run this:

class MyClass:
    def __init__(self):
        self.mIntValue = 1024
        self.mStringValue = "hust";

if __name__ == "__main__":
    c = MyClass()
    import sys
    print sys.getsizeof(c),sys.getsizeof(MyClass)

The output is still 72 and 104, but I added two extra properties, so I guess that the object should become "bigger". Well, the result seems not so.

6
  • Which Python version? On 3.3 I get 32 for c and 488 for MyClass. Commented Jun 13, 2013 at 14:34
  • Then MyClass is an old-style class. Those should be avoided, and I wouldn't be surprised if that somehow accounts for most of those 72 bytes. Commented Jun 13, 2013 at 14:36
  • 1
    class MyClass(object): pass when I wrote like this, the object is 64 and the class is 904... Commented Jun 13, 2013 at 14:37
  • My guess is that getsizeof doesn't include the size of __dict__. Commented Jun 13, 2013 at 14:46
  • You should take a look in here (see PyObject and PyTypeObject). Also, sizeof works like in C: it does not "follow" pointers (a const char * is always the size of a pointer, even if it points to a 1Gb memory space). Commented Jun 13, 2013 at 14:48

1 Answer 1

9

Basically, pythons classes and instances are both objects. When you test for the size of MyClass(), you're querying the memory size of an instance object, and when you do it for MyClass, there you are testing the size of a class object.

Both objects has their own fields, that's why the size is different, and it's no surprise that the class object needs more memory than the instance.

The instance of a class in python is a sort of dictionary that associate names to python objects (functions, fields, etc). In fact, they are stored in MyClass().__dict__ dictionary. So, if you add more fields to your class, the size won't change, because the class points to the __dict__ object who stores the fields in turn.

Testing for sys.getsizeof(MyClass().__dict__) yields also a fixed lenght. Because the python dictionary stores its keys in a keys list and its values in a values list (with a smart hash association, etc).

So if you want to see the class size growing you may do:

Dump from ipython

In [11]: class A():
   ....:     def __init__(self, **kwargs):
   ....:         for k,a in kwargs.items():
   ....:             self.__dict__[k]=a
   ....:
   ....:

In [14]: a1 = A(a=2)

In [15]: a2 = A(a=2,b=3,c='aaaa')

In [16]: import sys

In [17]: sys.getsizeof(a1.__dict__)
Out[17]: 140

In [18]: sys.getsizeof(a2.__dict__)
Out[18]: 140

In [19]: sys.getsizeof(a1.__dict__.keys())
Out[19]: 40

In [20]: sys.getsizeof(a2.__dict__.keys())
Out[20]: 48

In [21]: sys.getsizeof(a2.__dict__.keys()+a2.__dict__.values())
Out[21]: 60

In [22]: sys.getsizeof(a1.__dict__.keys()+a2.__dict__.values())
Out[22]: 52

Hope this can explain something to you.

Sign up to request clarification or add additional context in comments.

4 Comments

So basically, sys.getsizeof() is completely useless (except when trying to determine the cost of a modification to the interpreter).
Is not useless, from my point of view it works exactly as sizeof in C. It doesn't follow references to get the size, just return the size of the pointer who points to the referenced object. There are other tool to check memory in python. You can try them!
Since everything is a pointer, sys.getsizeof() basically only tells you how many pointers are in your object. You get something like C+K*n where C is the base object size, K is the size of one pointer and n is the number of child elements (list items, etc.). And in the case of a dict, it gives you a constant. This hardly tells anyone anything. In C, it may not follow pointers, but it can tell you a whole lot about lots of things that aren't pointers (with no such equivalent in Python).
You're right! I was just trying to illustrate it with a wide used example. sizeof is a very important function in C, sys.getsizeof()is a barely used function in Python IMHO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.