奇怪的 内存 释放问题。delete 之后,内存无法释放

J_流儿 J_流儿 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <iostream>
#include <map>
using namespace std;

int main(int argc, char* argv[])
{
int i = 0;
std::cout << "waitting for input, you can check current memory" << std::endl;
std::cin >> i;
char** ptr = new char *[1000000];
std::map<int, char *> tMap;

for (unsigned long i = 0; i < 1000000; i ++)
{
ptr[i] = new char[3000];
tMap.insert(make_pair(i, ptr[i]));
}
std::cout << "waitting for input, you can check current memory" << std::endl;
std::cin >> i;
for (unsigned long i = 0; i < 1000000; i ++)
{
delete []ptr[i];
}
delete []ptr;
std::cout << "waitting for input, you can check current memory" << std::endl;
std::cin >> i;
return 0;
}


上面代码,运行到 26行的时候,内存不会释放,一直显示进程占用内存 2.9G。
但是如果 删除 第 16行,delete 之后,内存就恢复到 几十M了。

我尝试 MALLOC_MMAP_MAX_=1000000 MALLOC_MMAP_THRESHOLD_=1024 ./memtest
命令来运行 , 结果还是一样的。在 第16行存在的时候,内存一直无法释放。
...全文

相关推荐

评论 16
收藏 已收藏
分享
返回顶部
J_流儿
C++ 语言 65,212 成员 250,513 内容