Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 地图的可用内存<;int,string>;_C++_Memory_Map - Fatal编程技术网

C++ 地图的可用内存<;int,string>;

C++ 地图的可用内存<;int,string>;,c++,memory,map,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

我有一个计划:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
地图m;
对于(int i=0;i<1000000;i++)
{
m[i]=“jahsdghsagdfv sahgvsahgd fvsahgdf fsdfjsadvhjgsd JHGFVSAFH ASFVASGFF jhgfdvsahgvfs”;
}
m、 清除();
而(1){sleep(5);}
返回1;
}

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

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

您使用的内存监视器是什么?你说它什么都不做是什么意思?它从映射中删除所有元素。这有许多重复项,但答案是
映射释放(或可能释放)内存,但仍映射到您的进程,以防它需要再次分配内存。某种程度上是一种优化。如果另一个进程需要更多内存,它可能会占用进程中的可用内存。一切正常。你的平台和程序之间的交互不是你所想的。我知道有很多关于这个的话题,但我找不到正确明确的答案。如果你绝对想控制进程消耗的确切内存量,你不能使用内存分配器(new/malloc),因为你依赖于他的算法,请参见
man sbrk
,但我很确定,当您了解要实现您似乎想要的目标需要做什么时,您的最终结论将是“目前的行为很好”: