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

▼魔方 西西 提交于 2020-01-13 10:06:36

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/4448897/what-is-the-real-memory-size-occupied-by-a-dynamic-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!