1

「person_map_tmulti_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 somthing、少なくとも1つの兆候。
  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)
4

2 に答える 2

1

multi_index_container の詳細はわかりませんが、そうなるべきだと思います。これは、boost や C++ に関するものよりも、使用するプラットフォームに関連しています。

Person の ctors と dtors にログを記録すると、Persons がまだ存在するかどうかを監視できますが、そうではないのではないかと思います。

プロセスが実際にオペレーティング システムにメモリを返したかどうかは、プラットフォームによって異なりますが、通常、言語自体とオペレーティング システムの間に、メモリを保持できるメモリ管理の層があると思います。

また、エミールが指摘したように、stl および boost クラスは、メモリを保持できるランタイムの上の追加レイヤーであるアロケーターを使用します。

于 2012-06-20T17:15:56.590 に答える
-1

メモリの解放は、消去を使用するときではなく、範囲外のときに発生します。それをスコープ外に出すには、それを囲み、それを使用するコードと{ }メモリ}が解放されます。

于 2012-06-20T16:10:57.277 に答える