By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

How much memory is used by a vector of maps?

P: n/a
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[i].size()*(sizeof(unsigned int)+sizeof(double));

---- END SNIPPET ----

Would appreciate your comments and suggestions as a sanity check.

Thanks in advance!
--
Murtaza Gulamali

Jun 18 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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[i].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
Jun 18 '07 #2

P: n/a
On Jun 18, 1:19 pm, mygulamali <mygulam...@gmail.comwrote:
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[i].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:ja*********@gmail.com
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

Jun 18 '07 #3

P: n/a
On 18 Jun, 12:19, mygulamali <mygulam...@gmail.comwrote:
>
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[i].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

Jun 18 '07 #4

P: n/a
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

Jun 19 '07 #5

P: n/a
On 19 Jun, 10:18, mygulamali <mygulam...@gmail.comwrote:
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

Jun 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.