Page 1 of 1

How to get object overall size

Posted: 16 Jan 2014, 13:38
by cyruz
Hi guys,
if I have an object I can find the size of the fields calling Object.GetCapacity(key), but how to find the overall size of the object itself? Is it safe to assume that the overall size is the sum of all fields size?

Re: How to get object overall size

Posted: 17 Jan 2014, 03:17
by lexikos
Why would you want to do that?

The overall memory usage would be the sum of:
- the capacity of each string field
- the size of each string key (including null-terminator)
- the capacity of the object multiplied by A_PtrSize=4 ? 16 : 24
- the size of the object's "root" structure - A_PtrSize*8
- overhead for each of the above memory allocations (depends on the OS and the size of the allocation)

Re: How to get object overall size

Posted: 17 Jan 2014, 08:05
by cyruz
lexikos wrote:Why would you want to do that?

The overall memory usage would be the sum of:
- the capacity of each string field
- the size of each string key (including null-terminator)
- the capacity of the object multiplied by A_PtrSize=4 ? 16 : 24
- the size of the object's "root" structure - A_PtrSize*8
- overhead for each of the above memory allocations (depends on the OS and the size of the allocation)
I was trying to understand if an object can be serialized in binary and stored, but looks like this is not possible; I guess that they are not allocated sequentially right? Anyway nice info!

Re: How to get object overall size

Posted: 18 Jan 2014, 03:09
by VxE

Re: How to get object overall size

Posted: 18 Jan 2014, 06:28
by cyruz
Yes, that's what I'm doing now. I was just curious. @lexikos, is this a feature that you have in your "todo list" for v2?

Re: How to get object overall size

Posted: 20 Jan 2014, 03:12
by lexikos
No, v2 is not about new features - see http://ahkscript.org/v2/

There's no reason you couldn't use a binary data format to serialize an object. You'd have to go through the same sort of process as when you serialize to JSON, but choose a binary format instead of text. But I suppose rather than serializing the object, you were actually hoping to just copy the object data in one go. That won't work because as you guessed, an object isn't just one continuous block of memory.

Re: How to get object overall size

Posted: 20 Jan 2014, 12:32
by cyruz
lexikos wrote:No, v2 is not about new features - see http://ahkscript.org/v2/

There's no reason you couldn't use a binary data format to serialize an object. You'd have to go through the same sort of process as when you serialize to JSON, but choose a binary format instead of text. But I suppose rather than serializing the object, you were actually hoping to just copy the object data in one go. That won't work because as you guessed, an object isn't just one continuous block of memory.
Yes I was hoping that! A ToBin() method would be a nice feature... Again, thank you for the info lexikos :)