C++对象的内存消耗

4 c++ memory memory-leaks new-operator

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

class Foo
{

};

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

    for (int i = 0; i < 10000000; i++)
        new Foo();
}
Run Code Online (Sandbox Code Playgroud)

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

UPDATE.我在Visual Studio 2010中对Win10 x64进行了测试.来自OS任务管理器的内存使用情况.

Tho*_*ler 5

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

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

附加内存用于无域(0xFDFDFDFD),对齐到16字节边界(0xBAADF00D),堆管理等.

我建议阅读这篇文章并查看调试器中的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!
}
Run Code Online (Sandbox Code Playgroud)