new不分配内存

Man*_*d3r 4 c++ memory

这应该每秒填充我的记忆大约100 MB.我用gnome-systemmonitor和htop跟踪内存.但不知怎的,它没有.为什么?

#include "unistd.h"
#include <iostream>

int main(int argc, char *argv[])
{
    while (true){
        std::cout << "New one" << std::endl;
        new char[100000000];
        sleep(1);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

跑步:

g++ -std=c++11 -O0  main.cpp; ./a.out  
Run Code Online (Sandbox Code Playgroud)

Min*_*ine 8

因为您没有使用它,所以Linux会进行延迟分配,因此在您使用之前它不会实际映射任何内存页面.

如果你把一些代码,如:

char* test = new char[100000000];
test[0] = 'a';
test[4096] = 'b';
...
Run Code Online (Sandbox Code Playgroud)

您应该看到它实际上消耗了您的系统内存.