How much memory is used by a vector of maps?

Discussion in 'C++' started by mygulamali, Jun 18, 2007.

  1. mygulamali

    mygulamali Guest

    Hi all!

    So I'm trying to determine how much memory is being used by the
    following class member in my code:

    vector<map<unsigned int, double> >

    Would the snippet below be the right way of going about this?

    ---- BEGIN SNIPPET ----

    vector<map<unsigned int,double> > my_data

    // fill my_data up with some, erm... data, then...

    int mem_used(0);
    mem_used += sizeof(vector<map<unsigned int,double> >);
    mem_used += my_data.capacity()*sizeof(map<unsigned int,double>);
    for (int i=0; i<my_data.size(); i++) mem_used +=
    my_data.size()*(sizeof(unsigned int)+sizeof(double));

    ---- END SNIPPET ----

    Would appreciate your comments and suggestions as a sanity check.

    Thanks in advance!
    --
    Murtaza Gulamali
     
    mygulamali, Jun 18, 2007
    #1
    1. Advertising

  2. mygulamali wrote:
    > So I'm trying to determine how much memory is being used by the
    > following class member in my code:
    >
    > vector<map<unsigned int, double> >
    >
    > Would the snippet below be the right way of going about this?
    >
    > ---- BEGIN SNIPPET ----
    >
    > vector<map<unsigned int,double> > my_data
    >
    > // fill my_data up with some, erm... data, then...
    >
    > int mem_used(0);
    > mem_used += sizeof(vector<map<unsigned int,double> >);
    > mem_used += my_data.capacity()*sizeof(map<unsigned int,double>);
    > for (int i=0; i<my_data.size(); i++) mem_used +=
    > my_data.size()*(sizeof(unsigned int)+sizeof(double));
    >
    > ---- END SNIPPET ----
    >
    > Would appreciate your comments and suggestions as a sanity check.


    The only sure way would be very platform- and compiler-specific.
    Many platforms and different implementations allow you to obtain
    the amount of "available" memory by calling a specific function
    (not a standard C++ function). Call it before and after your object
    is initialised, and that should give you the closest reading on the
    memory consumption of your particular type. Any other attempts,
    either involving 'sizeof' or any other means do not account for
    possible other allocations 'vector' or 'map' does to exist.

    IOW, to get the full picture on implementation-specific behaviour
    you need implementation-speicific means.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 18, 2007
    #2
    1. Advertising

  3. mygulamali

    James Kanze Guest

    On Jun 18, 1:19 pm, mygulamali <> wrote:
    > So I'm trying to determine how much memory is being used by the
    > following class member in my code:


    > vector<map<unsigned int, double> >


    You can't. It's unspecified, and there's not really any way of
    getting a reliable measure. (You can provide custom allocators,
    which track, but that doesn't tell you how much you use with the
    standard allocators, and of course, how much you use may depend
    on how you've filled the array, etc.)

    > Would the snippet below be the right way of going about this?


    > ---- BEGIN SNIPPET ----


    > vector<map<unsigned int,double> > my_data


    > // fill my_data up with some, erm... data, then...
    >
    > int mem_used(0);
    > mem_used += sizeof(vector<map<unsigned int,double> >);
    > mem_used += my_data.capacity()*sizeof(map<unsigned int,double>);
    > for (int i=0; i<my_data.size(); i++) mem_used +=
    > my_data.size()*(sizeof(unsigned int)+sizeof(double));


    > ---- END SNIPPET ----


    It will give you a minimum size, but it doesn't take into
    account all of the additional overheads that are be present.

    --
    James Kanze (GABI Software, from CAI) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 18, 2007
    #3
  4. mygulamali

    dasjotre Guest

    On 18 Jun, 12:19, mygulamali <> wrote:
    >
    > vector<map<unsigned int,double> > my_data
    >
    > // fill my_data up with some, erm... data, then...
    >
    > int mem_used(0);
    > mem_used += sizeof(vector<map<unsigned int,double> >);
    > mem_used += my_data.capacity()*sizeof(map<unsigned int,double>);


    the only think standard says is that capacity() returns
    number of objects that vector can store without requiring
    reallocation. nothing about the total allocated memory.

    > for (int i=0; i<my_data.size(); i++) mem_used +=
    > my_data.size()*(sizeof(unsigned int)+sizeof(double));


    sizeof(map<unsigned int,double>::value_type) does
    not have to be equal to sizeof(unsigned int)+sizeof(double)
    sizeof(map<unsigned int,double>::value_type) depends on
    its implementation and its alignment.

    > Would appreciate your comments and suggestions as a sanity check.


    If you want to find out exact memory requirement you
    will have to write your own allocator that tracks allocated
    size and use it for both your vector and map.

    regards

    DS
     
    dasjotre, Jun 18, 2007
    #4
  5. mygulamali

    mygulamali Guest

    Thank you all for your replies!

    Ok, so I understand that I will probably need to write my own
    allocators to assess true and precise memory usage. But do you think
    the snippet I submitted above would be sufficient to assess the
    maximum amount of memory that is being allocated for use by my object
    (the vector of maps)?

    In any case, I guess I'll have to get myself a copy of Intel VTune
    Analyzer and perform some profiling tests on my code.

    Thanks again!
    --
    Murtaza Gulamali
     
    mygulamali, Jun 19, 2007
    #5
  6. mygulamali

    dasjotre Guest

    On 19 Jun, 10:18, mygulamali <> wrote:
    > Thank you all for your replies!
    >
    > Ok, so I understand that I will probably need to write my own
    > allocators to assess true and precise memory usage. But do you think
    > the snippet I submitted above would be sufficient to assess the
    > maximum amount of memory that is being allocated for use by my object
    > (the vector of maps)?


    not maximum, not real, only theoretical minimum.

    > In any case, I guess I'll have to get myself a copy of Intel VTune
    > Analyzer and perform some profiling tests on my code.
    >
    > Thanks again!
    > --
    > Murtaza Gulamali
     
    dasjotre, Jun 19, 2007
    #6
    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. Simon Elliott
    Replies:
    4
    Views:
    1,174
    Simon Elliott
    Mar 10, 2005
  2. Bernard Lebel

    How much memory used by a name

    Bernard Lebel, Feb 14, 2007, in forum: Python
    Replies:
    7
    Views:
    261
    Bart Ogryczak
    Feb 15, 2007
  3. Replies:
    2
    Views:
    428
  4. Marcus
    Replies:
    2
    Views:
    598
    Marcus
    Dec 9, 2005
  5. Replies:
    8
    Views:
    1,970
    Csaba
    Feb 18, 2006
Loading...

Share This Page