Recursive Class Instance Size in Delphi

后端 未结 5 530
北海茫月
北海茫月 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:25

    to find out how much memory it uses, you could let the objects not get freed & let FastMM tell you the size of the leak.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-14 02:28

    No what you want does not exist. If you want something like that, you should write it yourself.

    0 讨论(0)
  • 2021-01-14 02:37

    Is there a way to get the total size of the class including its reference to other class instances?

    You just said it. The reference is a pointer; its size is 4 bytes. The value returned by InstanceSize is the number of bytes allocated for instance data of the class.

    myOtherVar2 might be nil, for example. But the nil pointer value would still occupy 4 bytes of memory.

    0 讨论(0)
  • 2021-01-14 02:49

    It sounds like you want to count memory used by your objects.

    If you need to do that, you can check how FastMM does it, and may be hook your procedure when objects of your type get created.

    A lot of work with unclear goal; You better have a good reason before starting it.

    0 讨论(0)
提交回复
热议问题