Is there a way to get the actual size of a class instance in Delphi?
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:
type MyClass1 = class
private
myVar1 : integer;
myVar2 : integer;
end;
type MyClass2 = class
private
myOtherVar1 : integer;
myOtherVar2 : MyClass1;
end;
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.
Is there a way to get the total size of the class including its reference to other class instances?
Construct one MyClass1 object and a million MyClass2 such that each MyClass2 points to the same MyClass1.
How much memory does each MyClass2 take? 12.000012 bytes?
How much memory does a circular list take? Infinity as you can keep chasing pointers for ever?
In languages with pointers, a naive recursive size-of algorithm isn't useful in general. You need to write your own algorithm which embodies knowledge about the aggregation/composition, sharing and recursive references specific to how you're using the objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With