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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:01:50+00:00 2026-06-10T09:01:50+00:00

Suppose, I have a very large std::map< unsigned int, Foo > FooDB , which

  • 0

Suppose, I have a very large std::map< unsigned int, Foo > FooDB, which holds Foo objects in memory, retrievable by their ID. Now there might be more Foo objects than there is memory available to store them. So I’d like to have the following construct:

  • retrieve Foo object with ID x from FooDB
  • if object x is in FooDB, return it
  • if it isn’t, load it from HD, try to store it in FooDB for further queries
    • there is enough memory available: add it to FooDB
    • there is not enough memory: free some space by removing from FooDB objects not in use (oldest query timestamp)

I’d like to reserve some memory for the FooDB and I can’t tell, how many Foo objects can be stored in it, as they differ in size.

Any ideas on how to implement this?

EDIT

My basic problem is: how can I tell a std::map‘s size in memory? All heap objects stored in it included, of course. How can I know when the not enough memory part has been reached?

  • 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-10T09:01:52+00:00Added an answer on June 10, 2026 at 9:01 am

    As far as I know, there’s no way to ask an object what its size is, other than sizeof(). You said that sizeof() won’t work because the Foo objects don’t have a fixed size. In that case, if you can modify Foo, then maybe your Foo class can keep track of its memory footprint internally. And if you can’t modify Foo, you might be able to write an external function that can deduce the memory footprint.

    Fundamentally, it would be very difficult for the language/compiler/runtime to know how big a dynamically-sized object is because it doesn’t know which allocations belong to the object. A simple solution, just recursively sum all of the things that it’s members point to, will fail on anything that has a pointer to an object that it doesn’t “own”. Another simple solution, to keep track of all of the allocations done between when the constructor starts and when it returns, will fail for anything that makes allocations after the constructor is called.

    You might want to just use the number of Foo’s as your cache limit instead of the memory size. Unless you know a lot about the memory availability and usage of the entire system, a cap based on memory size would be arbitrary as well. And if you know a lot about the memory usage of the entire system, you could just use the overall memory availability to determine when to release objects from the cache.

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

Sidebar

Related Questions

Suppose I'm using the MATLAB IDE and happen to have some very large objects
I have a very large list Suppose I do that (yeah, I know the
Suppose I have a very large site like amazon.com. I’d like to use asp.net
I have a very large object which I wish to serialize. During the process
Suppose, I have very large foreach loop. I want put this loop into try
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
Suppose we have a table which holds information about person. Columns like NAME or
I have a python app which is supposed to be very long-lived, but sometimes
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a class Baz that inherits from classes Foo and Bar ,

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.