Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:10:23+00:00 2026-06-06T04:10:23+00:00

How to free the memory used by the person_map_t multi_indexed_persons_map? I cant find something

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:10:24+00:00Added an answer on June 6, 2026 at 4:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I obtain this information: Total Memory Free Memory Memory used by current
As I understand it MEM_RESERVE is actually 'free' memory, ie available to be used
Out of pure curiosity, is there a way to free the memory used by
How does free calculate used memory and why does it differ from what /proc
Is there a way to free up memory used by required gems? My Rails
all: here is my server memory info with 'free -m' total used free shared
How can I find the memory used on my Android application, programmatically? I hope
Anybody knows how to find out how much physical memory is used and available
can swap function be used to free memory as in case of vectors as
I'm trying to free memory that's reallocated but I get an error... float *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.