title: Measure Object Size Accurately in Python - Sys.GetSizeOf not functioning
body: <p>I am trying to accurately/definitively find the size differences between two different classes in Python. They are both new style classes, save for one not having <strong>slots</strong> defined. I have tried numerous tests to determine their size difference, but they always end up being identical in memory usage.</p>
  <p>So far I have tried sys.GetSizeOf(obj) and heapy's heap() function, with no positive results. Test code is below:</p>
  <pre><code>import sys
  from guppy import hpy
  
  class test3(object):
      def __init__(self):
          self.one = 1
          self.two = &quot;two variable&quot;
  
  class test4(object):
      __slots__ = ('one', 'two')
      def __init__(self):
          self.one = 1
          self.two = &quot;two variable&quot;
  
  test3_obj = test3()
  print &quot;Sizeof test3_obj&quot;, sys.getsizeof(test3_obj)
  
  test4_obj = test4()
  print &quot;Sizeof test4_obj&quot;, sys.getsizeof(test4_obj)
  
  arr_test3 = []
  arr_test4 = []
  
  for i in range(3000):
      arr_test3.append(test3())
      arr_test4.append(test4())
  
  h = hpy()
  print h.heap()
  </code></pre>
  <p>Output:</p>
  <blockquote>
  <pre><code>Sizeof test3_obj 32
  Sizeof test4_obj 32
  
  Partition of a set of 34717 objects. Total size = 2589028 bytes.
   Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
       0  11896  34   765040  30    765040  30 str
       1   3001   9   420140  16   1185180  46 dict of __main__.test3
       2   5573  16   225240   9   1410420  54 tuple
       3    348   1   167376   6   1577796  61 dict (no owner)
       4   1567   5   106556   4   1684352  65 types.CodeType
       5     68   0   105136   4   1789488  69 dict of module
       6    183   1    97428   4   1886916  73 dict of type
       7   3001   9    96032   4   1982948  77 __main__.test3
       8   3001   9    96032   4   2078980  80 __main__.test4
       9    203   1    90360   3   2169340  84 type
  &lt;99 more rows. Type e.g. '_.more' to view.&gt;
  </code></pre>
  </blockquote>
  <p>This is all with Python 2.6.0. I also attempted to override the class's <strong>sizeof</strong> methods to try determine the size by summing the individual sizeofs but that didn't yield any different results:</p>
  <pre><code>class test4(object):
      __slots__ = ('one', 'two')
      def __init__(self):
          self.one = 1
          self.two = &quot;two variable&quot;
      def __sizeof__(self):
          return super(test4, self).__sizeof__() + self.one.__sizeof__() + self.two.__sizeof__()
  </code></pre>
  <p>Results with the <strong>sizeof</strong> method overridden:</p>
  <blockquote>
  <pre><code>Sizeof test3_obj 80
  Sizeof test4_obj 80
  </code></pre>
  </blockquote>