//使用for erase //for (auto iter = needClearMap.begin(); iter != needClearMap.end(); iter = needClearMap.erase(iter)) {} double sec = double(clock() - startt) / CLOCKS_PER_SEC; std::cout << "In Clear Cost Time:" << sec << endl; void test() map needClearMap; for (size_t i = 0; i <= 10000; ++i) useTest uT; for (size_t ii = 0; ii <= 1000; ++ii) uT.testMap[to_string(ii)] = "我是测试,我是测试,我就是测试string"; uT.testVertor.push_back("我也是测试,我也是测试,我就是测试string"); uT.id = to_string(i); //cout << i << endl; needClearMap[i] = uT; clock_t startt = clock(); clearData(needClearMap); double sec = double(clock() - startt) / CLOCKS_PER_SEC; std::cout << "clearData Cost Time:" << sec << endl; int main() for (size_t i = 0; i < 10; ++i) test(); getchar();

就单单实现某个map清空来说,swap效率最高,几乎是0耗时。但是当退出整个函数,释放swap转移到的临时对象要耗一定的时间。
erase效率稍微比clear高。通过for循环erase好似效率又高点。

内存释放:

STL容器调用clear()方法,通常只是使得容器内部的对象通通析构,但容器本身的内存无法得到释放。即篮子里面东西拿走了,篮子占的空间还在,这样是为了方便下次存放新的对象时,不需要再次申请空间。
其他同学的blog中有很多例子,即clear()后,容器的size为0,但capacity不变,

  • 通过swap()空容器,来彻底释放容器占用的capacity.

对于map,set,unordered_map等容器,调用clear(), swap()都无法使得内存真正释放。虽然很多地方谈到,这一现象(内存被保留下来)是正常的,并不需要担心。但是当大量使用堆内存存放不同的数据结构,会造成严重的内存碎片从而导致内存泄漏问题。

只需添加一行,malloc_trim(0); 这一行代码会将空闲的堆内存归还给操作系统,供其他进程使用。

#include <iostream>
#include <map>
#include <malloc.h>
using namespace std;
void func()
        map<int,string> mp;
        int i = 5000000;
        while(i--)
            mp.insert(make_pair(i,string("hell000o")));
        map<int,string>().swap(mp);
int main()
        func();
        cout <<"done."<<endl;
        malloc_trim(0);
        while(1);
                    模拟实际生产情况的测试代码,map中的value保存的是对象。#include&lt;map&gt;#include&lt;vector&gt;#include&lt;string&gt;#include &lt;iostream&gt;#include &lt;time.h&gt;using namespace std;class useTest{public:    useTest() {};    map&lt;string,string&gt; testMap;    
				
最近遇到一个特别占内存的需求。使用STL map/unordered_map内存无法得到正确释放。再次响应请求,会出现内存溢出的情况。 [6453149.107435] Memory cgroup out of memory: Kill process 54949 (******) score 1001 or sacrifice child [6453149.117193] Killed p...
本文实例讲述了c++map的基本用法和嵌套用法。分享给大家供大家参考。具体分析如下: C++map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。本文主要总结一下map基本用法和嵌套用法示例。 一、map基本用法 1. 头文件 代码如下:#include <map> 2. 定义 代码如下:map<int> my_Map; //注意这里的int和int可以是其他类型 代码如下:typedef map<int> MY_MAP; MY_MAP my_Map; 3. 插入数据 (1) my_Map[1]   =
文章目录现实问题测试环境计算机情况IDE运行情况测试代码测试结果使用clear使用swap使用erase使用for erase测试结论最后问题解决 生产中接收历史股票行情做一些别的计算时。整个计算系统的大概流程如下: #mermaid-svg-29KtzBi1WEfg0ffS .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#m
参考链接:关于c++map内存占用问题   (原文)原文提到map在erase和clear后不会立马释放内存,而是采用类似内存池的机制,对申请的内存进行”预留“,在一段时间内不使用才会予以“释放”。   (在编译器MSVC1930的实际测试中)大内存空间在erase和clear后会立马释放   (猜测)小内存空间在erase和clear后会被内存池回收 delete+erase:编译器M stat.S_IRUSR: Read by owner. 对于拥有者读的权限 --> Read by user. 对于所有者读的权限 stat.S_IWUSR: Write by owner. 对于拥有者写的权限 --> Write by user. 对于所有者写的权限 stat.S_IXUSR: Execute by owner. 对于拥有者执行的权限 --> Execute by user. 对于所有者执行的权限。 因为USR 是user, 不是owner 不过这里的 user 是用“所有者”代替还是“用户”代替,这点我就不太清楚了 Process finished with exit code -1073740791 (0xC0000409) weixin_45771212: 感谢大佬,成功解决 golang 判断字符串是否为数字字符串 doNotTellU: [code=plain] if str == "" { return false [/code] WSL2常用命令、设置静态 IP 地址 qq_37225682: 操作前 window(ip) wsl(ip) ubuntu(ip) 能两两ping通 且能ping百度 照楼主 想固定wsl(ip)跟ubuntu(ip) 操作后 ubuntu(ip) ping不通window(ip) 也不能上网 防火墙开了也不行 wsl -d Ubuntu-22.04 -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0 wsl -d Ubuntu-22.04 -u root ip addr add 192.168.4.220/24 broadcast 192.168.4.255 dev eth0 wsl -d Ubuntu-22.04 -u root ip route add 0.0.0.0/0 via 192.168.4.215 dev eth0 wsl -d Ubuntu-22.04 -u root echo nameserver 192.168.4.215 ^> /etc/resolv.conf powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.4.215 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.4.0/24;" 操作前 window(192.168.4.230) wsl(172.X.X.X) ubuntu(172.X.X.X) 操作后 window(192.168.4.230) wsl(192.168.4.235) ubuntu(192.168.4.240) wsl 启动 [已退出进程,代码为 4294967295] 解决办法 不要剁我爪: 这个方法的本质是把在‘启用或关闭Windows功能’里把‘虚拟机平台’和‘适用于Linux的子系统’关闭,重启电脑再打开,再重启电脑。有用