Question

How to free the memory used by the "person_map_t multi_indexed_persons_map"? I cant find something about it on google.

This is my testcode:

#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;
}

How to test:

  1. Start the binary.
  2. Wait till its done inserting.
  3. Check memory consumption with ps u -C <binname>.
  4. cin somthing, at least one sign.
  5. Check memory consumption again with ps u -C <binname>.

It now should be freed some memory, right?

EDIT:

valgrind output:

==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)
Was it helpful?

Solution

It should and I guess it will, although I don't know the details of multi_index_container. This is more related to the platform you use than to anything regarding boost or C++.

If you put logging in the Person's ctors and dtors you can watch whether Persons are still around, I suspect they won't.

Whether the process actually returned memory to the operating system depends on the platform, but there usually is a layer of memory management between the language itself and the operating system that can hold on to memory I believe.

Also, as Emile pointed out, stl and boost classes use allocators that are an additional layer above the runtime that can also hold on to memory.

OTHER TIPS

Memory freeing happens when it's out of scope and not when you use erase. To get it out of scope surround it and the code that uses it with { } and after the } the memory will be freed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top