简体   繁体   English

有没有一种方法可以显示对象已分配的堆内存量?

[英]Is there a way to print the amount of heap memory an object has allocated?

In a running program, how can I track/print the amount of heap memory an object has allocated? 在正在运行的程序中,如何跟踪/打印对象已分配的堆内存量?

For example: 例如:

#include <iostream>
#include <vector>

int main(){

  std::vector<int> v;

  std::cout << heap_sizeof(v) << '\n';

  for (int i = 0; i < 1000; ++i){
    v.push_back(0);
  }

  std::cout << heap_sizeof(v) << '\n';
}

Is there an implementation that could substitute heap_sizeof() ? 有没有可以替代heap_sizeof()

First, v is allocated on the stack, not on the heap. 首先, v是在堆栈上分配的,而不是在堆上分配的。

To get the total amount of space used by it, I suggest using this function: (Found on this article , and modified a bit) 为了获得它使用的总空间,我建议使用以下函数:( 在本文中找到,并稍作修改)

template <typename T>
size_t areaof (const vector<T>& x)
{
   return sizeof (vector<T>) + x.capacity () * sizeof (T);
} 

If you want not to count the size of the std::vector object itself, the delete the part with sizeof : 如果您不想计算std::vector对象本身的大小,请删除带有sizeof的部分:

template <typename T>
size_t heap_sizeof (const vector<T>& x)
{
   return x.capacity () * sizeof (T);
} 

With everything as it's designed out of the box, no, that's not possible. 开箱即用的所有设计都是不可能的。 You do have a couple of options for doing that on your own though. 但是,您确实有几个选择可以自己完成。

If you need this exclusively for standard containers, you can implement an allocator that tracks the memory that's been allocated (and not freed) via that allocator. 如果您只需要标准容器专用的内存,则可以实现一个分配器,该分配器跟踪通过该分配器分配(而不是释放)的内存。

If you want this capability for everything allocated via new (whether a container or not) you can provide your own implementation of operator new on a global and/or class-specific basis, and have it (for example) build an unordered map from pointers to block sizes to tell you the size of any block it's allocated (and with that, you'll have to provide a function to retrieve that size). 如果您希望通过new (无论是否为容器)分配的所有功能都具有此功能,则可以在全局和/或特定于类的基础上提供自己的operator new实现,并使其(例如)根据指针构建无序映射块的大小以告诉您分配的任何块的大小(并且,您必须提供一个函数来检索该大小)。 Depending on the platform, this might also be implemented using platform-specific functions. 根据平台的不同,也可以使用平台特定的功能来实现。 For example, when you're building for Microsoft's compiler (well, library, really) your implementation of operator new wouldn't have to do anything special at all, and the function to retrieve a block's size would look something like this: 例如,当您为Microsoft的编译器(实际上是库)构建时,您对operator new的实现根本不需要做任何特殊的事情,并且用于检索块大小的函数如下所示:

size_t block_size(void const *block) { 
    return _msize(block);
}

Yet another possibility would be to increase the allocation size of each requested block by the size of an integer large enough to hold the size. 还有另一种可能性是将每个请求块的分配大小增加一个足以容纳该大小的整数的大小。 In this case, you'd allocate a bigger chunk of data than the user requested, and store the size of that block at the beginning of the block that was returned. 在这种情况下,您将分配比用户请求更大的数据块,并将该块的大小存储在返回的块的开头。 When the user requests the size of a block, you take the correct (negative) offset from the pointer they pass, and return the value you stored there. 当用户请求块的大小时,您需要从其传递的指针中获取正确的(负)偏移量,然后返回存储在其中的值。

If you are not concerned with accounting for what each object allocates and are more concerned with how much memory has been allocated/freed between to point in time, you can use the malloc statistics functions. 如果您不关心每个对象分配什么,而是更关心在某个时间点之间分配/释放了多少内存,则可以使用malloc统计功能。 Each malloc has its own version. 每个malloc都有其自己的版本。 On linux you can use mallocinfo() . 在Linux上,您可以使用mallocinfo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM