jpfollenius
jpfollenius

Reputation: 16602

Byte size of object including size of members

I want to know (for debugging and logging) the size of an object in bytes, but not like

Sizeof (Object)

but like a 'deep sizeof'. For example if the object contains a hash map or a list, the real size needed by that hash map or list should be added to the result. Is there any way to do this without having to add a

property Size : LongWord read GetByteSize;

to each and every little object?

Probably a stupid question, but I'll give it a try ;)

EDIT: Just found almost the same question:

Recursive Class Instance Size in Delphi

I guess, the question can be closed. Sorry about that!

Upvotes: 4

Views: 4394

Answers (2)

Loren Pechtel
Loren Pechtel

Reputation: 9083

Since this is for debugging, have you looked at the FastMM4 memory allocator? It's got some nice stuff for tracing memory leaks.

Upvotes: 0

Toon Krijthe
Toon Krijthe

Reputation: 53366

Unfortunately, you need to write the code for this yourself.

Not sure of this works, but you can become very dirty:

  • Find the object size in bytes. Using TObject.InstanceSize.
  • Cast each group of 4 bytes to a pointer and then check if it is a TObject. You should be able to do that. If it is a TObject, you should repeat the step.

Upvotes: 2

Related Questions