综合一句话:vector只要clear就会真正释放内存;map、set、unordered_map只能通过malloc_trim(0)才能真正释放内存
方法 | map | vector | set | unordered_map |
---|---|---|---|---|
clear() | × | ✅ | × | × |
clear()后malloc_trim(0) | ✅ | ✅ | ✅ | ✅ |
clear()后置空容器 | × | ✅ | × | × |
clear()后swap | × | ✅ | × | × |
//置空容器
test_map = map<int, string>();
//swap
map<int, string> null_map; test_map.swap(null_map);
//或
map<int, string>().swap(test_map);