get memory size for STL containers

Discussion in 'C++' started by Rares Vernica, Oct 2, 2007.

  1. Hello,

    How can I determine how much memory is used by various STL containers?

    For example, I have a vector<int>, I know that an int takes 4B and I
    have 10 elements in my vector. So, I know that that my vector will take
    at least 40B, but I would like to know more exactly. I would like to
    know how much more does the vector needs for its stuff.

    I also need to know the memory size for set and map (unordered_map).

    Thanks,
    Ray
     
    Rares Vernica, Oct 2, 2007
    #1
    1. Advertising

  2. * Rares Vernica:
    > Hello,
    >
    > How can I determine how much memory is used by various STL containers?
    >
    > For example, I have a vector<int>, I know that an int takes 4B and I
    > have 10 elements in my vector. So, I know that that my vector will take
    > at least 40B, but I would like to know more exactly. I would like to
    > know how much more does the vector needs for its stuff.


    capacity() gives you a pretty good indication (multiply by element size
    and add a few bytes overhead).


    > I also need to know the memory size for set and map (unordered_map).


    More difficult and non-portable.

    If it turns out to be a problem you can always hammer away with the
    nearest sledge-hammer, uh, debugging tools.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 2, 2007
    #2
    1. Advertising

  3. Rares Vernica

    Pete Becker Guest

    On 2007-10-01 15:46:44 -1000, Rares Vernica <> said:

    >
    > How can I determine how much memory is used by various STL containers?
    >
    > For example, I have a vector<int>, I know that an int takes 4B and I
    > have 10 elements in my vector. So, I know that that my vector will take
    > at least 40B, but I would like to know more exactly. I would like to
    > know how much more does the vector needs for its stuff.
    >
    > I also need to know the memory size for set and map (unordered_map).
    >


    Write your own allocator to track memory usage.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Oct 2, 2007
    #3
  4. Pete Becker wrote:
    > On 2007-10-01 15:46:44 -1000, Rares Vernica <> said:
    >
    >>
    >> How can I determine how much memory is used by various STL containers?
    >>
    >> For example, I have a vector<int>, I know that an int takes 4B and I
    >> have 10 elements in my vector. So, I know that that my vector will
    >> take at least 40B, but I would like to know more exactly. I would like
    >> to know how much more does the vector needs for its stuff.
    >>
    >> I also need to know the memory size for set and map (unordered_map).
    >>

    >
    > Write your own allocator to track memory usage.
    >


    If this is the way to go in order to find the size of the of an STL
    container, then, did anyone created such an allocator? Did anyone
    created an allocator that acts as the default one, but also keeps track
    of the number of allocated bytes? I assume this can also be done by
    starting from the default allocator and adding some counters.

    Thanks,
    Ray
     
    Rares Vernica, Oct 2, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sasser
    Replies:
    2
    Views:
    1,132
    jlchereau
    Nov 16, 2008
  2. Khalid
    Replies:
    5
    Views:
    1,905
    John Harrison
    Mar 3, 2004
  3. Tomasz Grobelny
    Replies:
    4
    Views:
    2,232
    Mike Wahler
    Jan 1, 2005
  4. Replies:
    14
    Views:
    3,175
    Gianni Mariani
    May 23, 2005
  5. Andrey Vul
    Replies:
    6
    Views:
    589
    James Kanze
    Oct 22, 2009
Loading...

Share This Page