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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:25:35+00:00 2026-05-25T16:25:35+00:00

I need to gather statistics about memory usage in my program. My code is

  • 0

I need to gather statistics about memory usage in my program.

My code is mostly written using STL.

Is there any way of learning how much memory is being consumed by an STL object?

For example,

string s1 = "hello";
string s2 = "hellohellohellohellohellohellohellohellohellohellohellohellohello";

How much memory is consumed by s1 and s2?
Obviously, sizeof(string)+s1.length() is not quite accurate.

  • 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-05-25T16:25:35+00:00Added an answer on May 25, 2026 at 4:25 pm

    If you’re willing to be slightly intrusive, you can make a custom allocator to track all heap usage by container type, or by type allocated. This is quite intrusive, but quite accurate as well, for determining memory usage. This does not track how much memory the heap itself takes though, as that is highly OS-dependent.

    template<class TrackType> 
    size_t* mem_used() {static size_t s = 0; return &s;}
    
    template<class T, class TrackType, class BaseAllocator = std::allocator<T> > 
    class TrackerAllocator : public BaseAllocator {
    public:
        typedef typename BaseAllocator::pointer pointer;
        typedef typename BaseAllocator::size_type size_type;
    
        TrackerAllocator() throw() : BaseAllocator() {}
        TrackerAllocator(const TrackerAllocator& b) throw() : BaseAllocator(b) {}
        TrackerAllocator(TrackerAllocator&& b) throw() : BaseAllocator(b) {}
        template <class U> TrackerAllocator(const typename TrackerAllocator::template rebind<U>::other& b) throw() : BaseAllocator(b) {}
        ~TrackerAllocator() {}
    
        template<class U> struct rebind {
            typedef TrackerAllocator<U, TrackType, typename BaseAllocator::template rebind<U>::other> other;
        };
    
        pointer allocate(size_type n) {
            pointer r = BaseAllocator::allocate(n);
            *mem_used<TrackType>() += n;
            return r;
        }
        pointer allocate(size_type n, pointer h) {
            pointer r = BaseAllocator::allocate(n, h);
            *mem_used<TrackType>() += n;
            return r;
        }
        void deallocate(pointer p, size_type n) throw() {
            BaseAllocator::deallocate(p, n);
            *mem_used<TrackType>() -= n;
        }
    };
    

    And usage is:

    typedef std::basic_string<char, 
                              std::char_traits<char>,
                              TrackerAllocator<char, std::string> > trackstring;
    typedef std::vector<int, 
                        TrackerAllocator<int, std::vector<int> > > trackvector;
    //                                        ^              ^
    //                                        This identifies which memory to track
    //                                        it can be any type, related or no.
    //                                        All with the same type will be tracked togeather
    int main() {
        trackstring mystring1("HELLO WORLD");
        std::cout << *mem_used<std::string>() << '\n'; //display memory usage of all strings
    
        trackstring mystring2("MUCH LONGER STRING THAT DEFINITELY GETS HEAP ALLOCATED!");
        std::cout << *mem_used<std::string>() << '\n'; //display memory usage of all strings
    
        trackvector myvec(mystring1.begin(), mystring1.end());
        std::cout << *mem_used<std::vector<int> >() << '\n'; //display memory usage of all vector<int>
        //                     ^              ^
        //                     This identifies which memory type from above to look up.
        return 0;
    }
    

    Windows results:

    0 //this is zero, because the string did not allocate heap space.
    64
    11

    http://ideone.com/lr4I8 (GCC) results:

    24
    92
    11

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

Sidebar

Related Questions

I need to gather some statistics in my software and i am trying to
I'm writing both client and server code using WCF, where I need to know
I need to gather some system information for the application I'm developing. The memory
I need a modal dialog to gather some user input. I then need the
How can I gather the visitor's time zone information? I need both: the time
Need a way to allow sorting except for last item with in a list.
need ask you about some help. I have web app running in Net 2.0.
Need some help about with Memcache. I have created a class and want to
I need to create an automated process (preferably using Java) that will: Open browser
I have a very large text file and I need to gather data from

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.