C++对象内存消耗

C++ objects memory consumption

首先:这个问题不是关于"how to use delete operator",而是关于"why many class objects of small size consumes lots of memory"。假设我们有这段代码:

class Foo
{

};

void FooTest()
{
    int sizeOfFoo = sizeof(Foo);

    for (int i = 0; i < 10000000; i++)
        new Foo();
}

空classFoo的大小是1个字节,但是当代码执行时它消耗了大约600Mb的内存。怎么样?

更新。 我在 Visual Studio 2010 年的 Win10 x64 上测试过这个。OS 任务管理器的内存使用情况。

Class Foo 的大小可能是 1 个字节,但由于您单独分配了许多 Foo,因此它们可以(并且可能确实)分配到一些字节对齐的地址上,并且由于碎片消耗的内存比您预期的多。

另外还有内部内存管理系统使用的内存

C++ 堆管理器有 4 个不同的 "modes",其中它在一个对象周围保留或多或少 space。这些是

  1. 释放模式,运行正常
  2. 发布模式,运行调试器下
  3. 调试模式,运行正常
  4. 调试模式,运行在调试器下

额外的内存用于无人区 (0xFDFDFDFD)、对齐到 16 字节边界 (0xBAADF00D)、堆管理等

我建议阅读 this post 并查看调试器中的 4 个场景,打开原始内存视图。你会学到很多东西。对于情况 1 和 3,插入一个暂停,您可以在其中将调试器附加到 运行ning 进程,而在情况 2 和 4 中,您应该先 运行 调试器,然后从那里启动可执行文件。

我在解释缓冲区over运行时曾经演示过C++堆是如何工作的。这是您可以使用的演示程序,虽然不完美,但可能有用:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <stdio.h>
void SimpleBufferOverrunDemo( int argc, _TCHAR* argv[] ) ;

int _tmain(int argc, _TCHAR* argv[])
{
    SimpleBufferOverrunDemo(argc, argv);

    getchar();
    return 0;
}

void SimpleBufferOverrunDemo( int argc, _TCHAR* argv[] ) 
{
    if (argc != 2)
    {
        std::cout << "You have to provide an argument!\n";
        return;
    }

    // Allocate 5 bytes
    byte* overrunBuffer = new byte[5];

    // Demo 1: How does the memory look after delete? Uncomment the following to demonstrate
    //delete [] overrunBuffer; //0xfeeefeee in debug mode.
    //DebugBreak();

    // Demo 2: Comment Demo 1 again. 
    // Provide a 5 byte sequence as argument
    // Attach with WinDbg and examine the overrunBuffer.

    // 2.1. How many heaps do we have?
    // !heap -s

    // 2.2. How to find the heap block and how large is it?
    // !heap -x [searchAddress]
    // !heap -i [blockAddress] -> Wow 72 bytes block size for 5 allocated bytes!

    // 2.3. Show that _HEAP_ENTRY does not work any more.

    // Demo 3: Write behind the 5 allocated bytes.
    // Provide more than 5 bytes as argument, depending on how what you want to destroy
    // 3.1 Write into the no mans land.
    // 3.2 Write into the guard bytes.
    // 3.3 Write into the meta data section of the following heap block! -> When does it crash?

    std::wstring arg = argv[1];

    for (size_t i = 0; i < arg.size(); i++)
    {
        overrunBuffer[i] = (byte)arg[i];
    }

    // Crash happens not where it was caused(!) This is important!
    std::cout << "Now we do a plenty of other work ...";
    ::Sleep(5000);

    delete[] overrunBuffer;

    // Demo 4: Demonstrate page heap / application verifier!
}

你要知道,从OS的角度来看,进程消耗的内存不仅仅与你代码中分配的对象有关。

这是一个严重依赖于实现的东西,但一般来说,出于性能原因,内存分配很少一对一地传递给 OS,而是 pooled via the management 的自由存储。总的原则如下:

  • 在程序启动时,您的 C++ 实现将为空闲存储区和堆分配一些初始 space。
  • 当消耗此内存并需要新内存时,内存管理将向 OS 请求更大的块,并在空闲存储中提供这些块。
  • OS 请求的块大小可能会适应分配模式。

因此,从任务管理器中查看的 600MB 来看,可能只有一小部分有效分配给了您的对象,而更大的部分实际上仍然是空闲的,可以在空闲存储区中使用。

也就是说,消耗的内存将大于对象的大小 x 数量:对于每个分配的对象,内存管理函数必须管理一些附加信息(如分配对象的大小)。同样,空闲内存池也需要指针(通常是链表)来跟踪空闲块(如果它们不连续)。

非常有趣 post 关于 Windows。

为了比较,Ubuntu 15.10(64):

int t407(void)
{
   std::cout << "\nsizeof(Foo): " << sizeof(Foo) << std::endl;

   std::cout << "\nsizeof(Foo*): " << sizeof(Foo*) << std::endl;

   std::vector<Foo>  fooVec;
   fooVec.reserve(10000000);
   for (size_t i=0; i<10000000; ++i)
   {
      Foo t;
      fooVec.push_back(t);
   }
   std::cout << "\nfooVec.size(): " << fooVec.size() 
             << " elements" << std::endl
             << "fooVec.size() * sizeof(Foo): " 
             << fooVec.size() * sizeof(Foo) << " bytes" << std::endl
             << "sizeof(fooVec): " << sizeof(fooVec) 
             << " bytes (on stack)" << std::endl;

   return(0);
}

输出:

 sizeof(Foo): 1

 sizeof(Foo*): 8

 fooVec.size(): 10000000 elements 
 fooVec.size() * sizeof(Foo): 10000000 bytes
 sizeof(fooVec): 24 bytes (on stack)