new不分配内存 [英] new does not allocate memory
本文介绍了new不分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这应该填充我的记忆每秒用ca 100 MB。我用gnome-systemmonitor和htop跟踪内存消耗。但不知何故,它不。为什么?
#includeunistd.h
#include< iostream&
int main(int argc,char * argv [])
{
while(true){
std :: cout< 新一个< std :: endl;
new char [100000000];
sleep(1);
}
return 0;
}
运行:
g ++ -std = c ++ 11 -O0 main.cpp; ./a.out
解决方案
如果你放了一些代码,如:
。如果你不使用它,那么它就不会映射任何内存页。
char * test = new char [100000000]
test [0] ='a';
test [4096] ='b';
...
您应该看到它实际上消耗了您的系统内存。
This should fill my memory every second with ca 100 MB. I track the memoryusage with gnome-systemmonitor and htop. But somehow it does not. Why?
#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;
}
To run:
g++ -std=c++11 -O0 main.cpp; ./a.out
解决方案
Because you're not using it, Linux does lazy allocation so it will not actually map any memory page until you use it.
If you put some code like:
char* test = new char[100000000];
test[0] = 'a';
test[4096] = 'b';
...
You should see it's actually consuming your system memory.
这篇关于new不分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文