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 8342589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:49:04+00:00 2026-06-09T05:49:04+00:00

I am working on an application in which I am planning to use couple

  • 0

I am working on an application in which I am planning to use couple of STL containers. The application will take certain steps if memory consumption reaches a threshold. For that purpose I need to perform a close to accurate calculation on how much memory is used by STL containers.

vector<string> StringList
map<string, int> mapstring

This is how I am estimating memory:

For size of StringList, loop over all the elements of the vector and keep adding the string sizes.

string size = sizeof(string) + string.capacity()*sizeof(char)

Then finally add to this sizeof(StringList);

For size of mapstring, loop over all the keys of the container and keep adding the string sizes and then add the sizes of int which is mapstring.size()*sizeof(int). Then finally add to this sizeof(mapstring);

I guess a better approach would be specifying own allocator class and keeping track of memory usage inside it but writing one could be non-trivial. Does this estimation look good ?

  • 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-09T05:49:05+00:00Added an answer on June 9, 2026 at 5:49 am

    For std::vector and std::string, capacity, rather than size, would
    be a better approximation. For node based containers (std::set,
    etc.), you’d want multiply the number of nodes (roughly the number of
    elements) times the size of each node. This only accurate, however, if
    the allocator doesn’t use an optimized pool allocator for the nodes.

    If you really want to know how much memory is being used, however, a
    better strategy would be to replace the global operator new and
    operator delete, and keep track of the actual allocations. Even more
    accurate would be to replace malloc and free. Formally, this is not
    allowed, but in practice, I’ve never encountered an implementation where
    it doesn’t work. On the other hand, if you replace malloc and free,
    you have to implement the actual memory management yourself. If you
    replace operator new and operator delete, you can use malloc and
    free, which makes it fairly trivial.

    Note too that each allocation has some fixed overhead. A 100000
    allocations of 10 bytes each will consume significantly more memory than
    10 allocations of 100000 bytes each.

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

Sidebar

Related Questions

I'm working on a I18N application which will be located in Japanese, I don't
I am working on an application that will have a library folder which will
I am working on application which will create some text files into the SD
I am working on application which needs to be authenticated in different domain. To
i am working on one application which requires the feature of adding the custom
I'm working on an application which allows employees to Request time off that post
We are working on an application which processes PDF documents. One of the features
I am working on an application which contain register activity as it's first page
I am working on an application which fetch the location details in a periodic
I am working on an application which starts as a service but only if

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.