How to get the memory size of a dynamic array?

In Delphi, you can get the size of a value type using the compiler's magic function sizeof() , but calling sizeof() on the reference type will give you the size of the pointer, not the value that it points to.

For objects, you can get the memory size using the InstanceSize method, but what about dynamic arrays? Due to indentation, length(MyArray) * sizeof(element) may be inaccurate. So, is there any exact way to get the memory size of a dynamic array?

+6
source share
2 answers

There is no indentation between the elements of the dynamic array, Length(MyArray)*SizeOf(Element) must be accurate.

+5
source

In fact, length(MyArray) * sizeof(element) will be accurate for the contents of the array, excluding any internal dynamic array or string.

If you want the entire array to use memory, including the size of the contents of nested reference types, you can use our TDynArray wrapper. It is able to serialize into binary any dynamic array, including reference counted elements (for example, dynamic arrays or strings). You have SaveTo / SaveToStream for this purpose, and you can get the actual size of all the content.

Take a look at this blog post that introduces this shell. It runs open source and runs from Delphi 5 to XE4, both on the Win32 platform and on Win64.

+4
source

Source: https://habr.com/ru/post/949250/


All Articles