实际上从堆中为一个对象分配了多少内存? [英] How much memory was actually allocated from heap for an object?

查看:192
本文介绍了实际上从堆中为一个对象分配了多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序使用太多的内存分配堆上的大量小对象。所以我想调查一下优化它的方法。该程序是用Visual C ++ 7编译的。

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 compiled with Visual C++ 7.

有没有办法确定实际分配给一个给定的对象多少内存?我的意思是当我调用 new 时,堆分配不少于必要的金额。

Is there a way to determine how much memory is actually allocated for a given object? I mean when I call new the heap allocates not less than the necessary amount. How can I find how much exactly has been allocated?

推荐答案

没有确切的答案,因为一些堆管理器可能使用不同的数量的内存用于相同大小的顺序分配。此外,(通常)没有直接的方法来测量特定分配所占用的字节数。

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.

您可以通过分配一定数量的相同项目大小(比如说,4096),并注意到使用的内存的差异。把后者除以前者会给你答案。请注意,此值会随操作系统,操作系统版本,操作系统版本而变化,有时调试应用程序可能会启用额外的堆跟踪,这会增加开销。在某些操作系统上,用户可以更改堆策略(即使用一个堆分配器作为默认值)。示例:Windows和pageheap.exe

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

只是FYI,Windows 32位上的默认(不是LFH)堆占用:

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


  • sizeof(your_type),四舍五入到DWORD边界,

  • prev / next / size的三个指针

  • 安全Cookie的两个指针

这篇关于实际上从堆中为一个对象分配了多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆