如何释放“person_map_t multi_indexed_persons_map”使用的内存?我在谷歌上找不到关于它的东西。
这是我的测试代码:
#include <sstream>
#include <ostream>
#include <stdio.h>
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/tag.hpp>
#include <boost/multi_index/ordered_index.hpp>
struct name {};
struct age {};
struct person
{
std::string name_;
int age_;
std::string name() const
{
return name_;
}
int age() const
{
return age_;
}
};
typedef boost::multi_index::multi_index_container<
person,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::tag<name>,
boost::multi_index::const_mem_fun<person, std::string, &person::name>
>,
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<age>,
boost::multi_index::const_mem_fun<person, int, &person::age>
>
>
> person_map_t;
int main(int argc, char *argv[])
{
std::string userinput;
{
person_map_t multi_indexed_persons_map;
for (int i = 0; i < 100000; i++)
{
person p;
p.name_ = std::string("MyName_") + boost::lexical_cast<std::string>(i);
p.age_ = i;
multi_indexed_persons_map.insert(p);
}
std::cout << "Done inserting." << std::endl;
std::cin >> userinput; // while this is blocking, check consumtion with: `ps u -C <binname>`
}
// multi_indexed_persons_map.erase(multi_indexed_persons_map.begin(), multi_indexed_persons_map.end()); /* dosnt work too ... */
std::cout << "Memory freed?" << std::endl;
// out of scope, memory should be freed now,
// check again with: `ps u -C <binname>` and compare.
std::cin >> userinput;
return 0;
}
如何测试:
- 启动二进制文件。
- 等到它完成插入。
- 使用 . 检查内存消耗
ps u -C <binname>
。 - cin somthing,至少一个标志。
- 使用 再次检查内存消耗
ps u -C <binname>
。
现在应该释放一些内存,对吧?
编辑:
valgrind 输出:
==2314==
==2314== HEAP SUMMARY:
==2314== in use at exit: 0 bytes in 0 blocks
==2314== total heap usage: 400,005 allocs, 400,005 frees, 16,489,069 bytes allocated
==2314==
==2314== All heap blocks were freed -- no leaks are possible
==2314==
==2314== For counts of detected and suppressed errors, rerun with: -v
==2314== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)