4

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!

Community
  • 1
  • 1
jpfollenius
  • 16,456
  • 10
  • 90
  • 156
  • Wouldn't be a good idea to add Delphi in the question name? – bogdan Dec 10 '09 at 16:42
  • That's what tags are for...I consider it kind of redundant to add the programming language both as a tag and to include it in the title of EVERY question. Probably a matter of taste. – jpfollenius Dec 10 '09 at 18:36
  • Great question! Everybody uses TObject but nobody has a clue about how much memory it actually takes. – Gabriel May 02 '19 at 14:39

2 Answers2

2

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.
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
  • I was afraid of that...I have to make changes all over the place which I wanted to avoid...DAMN *g*...thanks anyway! – jpfollenius Apr 06 '09 at 13:00
  • Added a possible solution, but it is very dirty and i'm not sure if you can do that. – Toon Krijthe Apr 06 '09 at 13:06
  • 1
    Um, it's harder than that. You have to consider circular references, interfaces, and data which looks like an object but in fact is not. I think you'll find this difficult/impossible to perform successfully. – Craig Stuntz Apr 06 '09 at 13:18
  • sorry, missed your comment, Craig. Sounds like its not worth the effort considering all potential problems. Maybe I'll give it a try anyway... – jpfollenius Apr 06 '09 at 13:21
  • How are you going to distinguish between a 32 bit integer and a 32 bit TObject pointer? An array of primes won't contain any integer that you'll mistake for a pointer, but anything else might. Or you might get a raw pointer to some byte buffer. – MSalters Apr 06 '09 at 13:53
  • What about RTTI? Can't that be helpful? – jpfollenius Apr 06 '09 at 14:01
  • ahrg...forget about my comment...the size of all published members isn't helpful at all...would work in java I guess but not in Delphi. what a shame – jpfollenius Apr 06 '09 at 14:08
0

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

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
  • I'm not looking for memory leaks but for the size of certain internal data structures since the size varies with the input data. – jpfollenius Apr 07 '09 at 06:15