How much memory is used by a vector of maps?

M

mygulamali

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!
 
V

Victor Bazarov

mygulamali said:
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
 
J

James Kanze

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.
 
D

dasjotre

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)
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
 
M

mygulamali

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!
 
D

dasjotre

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top