c++ - 如何释放 boost::multi_index::multi_index_container 使用的内存?

标签 c++ memory free boost-multi-index

如何释放“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;
}

如何测试:

  1. 启动二进制文件。
  2. 等待插入完成。
  3. ps u -C <binname>检查内存消耗.
  4. cin something,至少有一个迹象。
  5. 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)

最佳答案

它应该而且我猜它会,虽然我不知道 multi_index_container 的细节。这与您使用的平台有关,而不是与 boost 或 C++ 相关的任何内容。

如果你在 Person 的 ctors 和 dtors 中进行日志记录,你可以看到 Persons 是否还在,我怀疑他们不会。

进程是否实际将内存返回给操作系统取决于平台,但我相信在语言本身和可以保留内存的操作系统之间通常有一层内存管理。

此外,正如 Emile 指出的那样,STL 和 boost 类使用分配器,这些分配器是运行时之上的附加层,也可以保留内存。

关于c++ - 如何释放 boost::multi_index::multi_index_container 使用的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123644/

相关文章:

java - java对象的内存预留

android - 如何理解Android Studio中的内存分配?

.net - 如何将 Marshal.AllocHGlobal 分配的内存清零?

c - 为什么我可以释放内存两次,但在不同情况下却不能?

c++ - for 循环内的 while 循环

c++ - 文件夹路径序列化的最大字符长度

c++ - 本地覆盖字符串提取运算符

c++ - C/C++ 中的二进制转换

c - C 中 malloc 分配的意外大小输出

c - 需要帮助来理解 free() 函数在 C 中的工作原理