C ++对象的内存消耗

时间:2016-05-04 21:31:52

标签: c++ memory memory-leaks new-operator

首先:这个问题不是关于"如何使用删除操作符",它是关于"为什么许多小尺寸的类对象消耗大量内存"。我们假设我们有这段代码:

class Foo
{

};

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

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

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

更新。我已经在Visual Studio 2010中的Win10 x64上对此进行了测试。来自OS任务管理器的内存使用情况。

4 个答案:

答案 0 :(得分:5)

C ++堆管理器有4种不同的“模式”,它可以在对象周围保留更少或更多的空间。这些是

  1. 发布模式,正常运行
  2. 发布模式,在调试器下运行
  3. 调试模式,正常运行
  4. 调试模式,在调试器下运行
  5. 附加内存用于无人域(0xFDFDFDFD),对齐到16字节边界(0xBAADF00D),堆管理等。

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

    我曾经在演示缓冲区溢出时演示了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!
    }
    

答案 1 :(得分:3)

Foo的大小可能是1个字节,但由于你单独分配了许多Foo,它们可以(并且可能)在某些字节对齐的地址上分配,并且由于碎片消耗更多的内存比你预期的要好。

此外,还有内存管理系统使用的内存。

答案 2 :(得分:0)

您必须知道,从操作系统的角度来看,进程所消耗的内存不仅与代码中分配的对象有关。

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

  • 在程序启动时,您的C ++实现将为免费存储和堆分配一些初始空间。
  • 当消耗此内存并且需要新内存时,内存管理将向操作系统请求更大的块并使其在免费存储中可用。
  • 从OS请求的块的大小可能会使自己适应分配模式。

因此,从任务管理器中查看的600MB,可能只有一小部分被有效地分配给您的对象,而更大的部分实际上仍然是免费的,并且可以在免费商店中使用。

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

答案 3 :(得分:0)

关于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)
相关问题