Recursive Class Instance Size in Delphi

后端 未结 5 534
北海茫月
北海茫月 2021-01-14 02:17

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 i

5条回答
  •  天涯浪人
    2021-01-14 02:28

    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.

提交回复
热议问题