title: Recursive Class Instance Size in Delphi
body: <p>Is there a way to get the actual size of a class instance in Delphi?</p>
  
  <p>I know about the InstanceSize method of the TObject class but that method does not recursively invokes itself for object members. For example, let's say we have two classes:</p>
  
  <pre><code>type MyClass1 = class
    private
      myVar1 : integer;
      myVar2 : integer;
  end;
  
  type MyClass2 = class
    private
      myOtherVar1 : integer;
      myOtherVar2 : MyClass1;
  end;
  </code></pre>
  
  <p>for this segment of code, MyClass1 will be 12 bytes length (4 bytes for each integer plus 4 for the class overhead) and MyClass2 will be 24 bytes lengh (4 bytes for the class overhead, 12 bytes from myOtherVar2 and another 4 for the myOtherVar1 integer). Using InstanceSize will result on 12 bytes for each of them since myOtherVar2 is interpreted as a pointer (4 bytes) instead of as a class reference.</p>
  
  <p>Is there a way to get the total size of the class including its reference to other class instances?</p>