c++ - map 的空闲内存<int, string>

标签 c++ memory map

<分区>

我有一个程序:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;

int main() {
    map<int, string> m;

    for (int i = 0; i < 1000000; i++)
    {
        m[i] = "jahsdghsagdfv sahgvsahgd fvsahgdf fsdfjsadvhjgsd jhgfhsahfvsafh asfvasgfv jhgfdvsahgvfs";
    }
    m.clear();
    while (1) {sleep(5);}
    return 1;
}

clear() 什么都不做。在内存监视器中,我看到内存使用量为 184 Mb,清除后没有任何变化。为什么 ?如何清除 map 内存?

最佳答案

是的,map::clear 做了一些事情:“从 ma​​p container 中移除所有元素(已被销毁),使容器的大小为 0。” 数据也不会从堆栈/堆中删除,但这不会影响您,因为您将没有指向该过时数据的指针和类型转换。 可能当您重新填充映射时,该内存区域将被重用并更新为新值(如果它同时未被其他变量使用)。

关于c++ - map 的空闲内存<int, string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22350287/

相关文章:

C++ 在与完美转发相同的语句中多次访问右值引用

c# - 无法更改 VS 2015 C++ 项目中的 .NET 目标框架版本

c - 在 C 特定索引范围内分配

c - memwatch的内存损坏和崩溃问题

c - 读取简单 CSV 文件时出现段错误 - C

拖动 map 时MapControl Xaml元素滞后

c++ - 如何在 Xcode 3.2.3 中编译 C++ 程序?

c++ - Vector::erase 段错误

C++如何在std::map中找到最大的键?

java - Hibernate:将多对多映射到 Map