How much memory was actually allocated from heap for an object?

前端 未结 6 674
渐次进展
渐次进展 2020-12-30 17:09

I have a program that uses way too much memory for allocating numerous small objects on heap. So I would like to investigate into ways to optimize it. The program is compile

6条回答
  •  离开以前
    2020-12-30 17:18

    There is no exact answer, because some heap managers may use different amount of memory for sequential allocations of the same size. Also, there is (usually) no direct way to measure number of bytes the particular allocation took.

    You can approximate this value by allocating a certain number of items of the same size (say, 4096) and noting the difference in memory used. Dividing the later by the former would give you the answer. Please note that this value changes from OS to OS, and from OS version to OS version and sometimes Debug builds of your application may enable extra heap tracking, which increases the overhead. On some OSes users can change heap policies (i.e. use one heap allocator vs. another as default). Example: Windows and pageheap.exe

    Just FYI, the default (not LFH) heap on Windows 32-bit takes up:

    • sizeof(your_type), rounded up to DWORD boundary,
    • 3 pointers for prev/next/size
    • 2 pointers for security cookies

提交回复
热议问题