Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "real" memory size occupied by a dynamic array?

Tags:

delphi

Example:

procedure Test;
var
  AText: array of AnsiChar;
begin
  SetLength(AText, 7);
end;

Question

What is the real size of AText occupied in memory? Is it 7 + Cardinal size of its length, that is, 7 + 4 = 11 bytes?

like image 878
Astaroth Avatar asked Dec 15 '10 10:12

Astaroth


1 Answers

That plus 4 bytes reference count. And of course heapmanager overhead (which depends on delphi version and uses memory manager, which can easily be 12-16 bytes).

So that means:

  • sizeof(element)*elementcount
  • sizeof(refcount)
    • current implementations :sizeof(integer)=4
  • sizeof(elementnumber)
    • FPC actually stores highest element, not elementcount. Don't know about Delphi)
    • current implementations :sizeof(integer)=4
  • heap overhead.
    • At least the allocated size for the entire block.
    • Probably one or two pointers also (next block). But this depends on memory manager
    • many memory managers have a minimal blocksize of 16 or 32.
like image 121
Marco van de Voort Avatar answered Sep 28 '22 05:09

Marco van de Voort