Memory occupied by Map

A

alien.0101

Hi

I want to know the memory occupied by STL Map, Is there a way to do
that.

I have written a sample application and adding value to map doesnot
change size
map<int,int> mapInt2Int;
mapInt2Int[1]=2;
mapInt2Int[2]=2;
mapInt2Int[3]=2;
mapInt2Int[4]=2;
mapInt2Int[5]=2;
mapInt2Int[6]=2;
size_t sz= sizeof(mapInt2Int);


How can I find how much bytes does the map uses.

My main requirement is to store the map as a byte blob in database and
rettrieve back.

any thoughts???
 
A

alien.0101

well i think the solution to get size is to iterate the map and and
get size of each element
this is the only way i can this right now


basic idea is i need to serialize the map contents so planning to
store as byte blob in db
 
G

Greg Herlihy

You can't do it in any portable way. If you want a solution specific to
your environment, you will have to ask in a newsgroup that specializes
in such information.

Instantiating the std::map with a custom Allocator (one that kept a
running total of the map's memory usage) should be able to provide at
least an upper bound on how much memory the database entry would need
in order to store all of the map's keys and values.

Greg
 
A

alien.0101

Instantiating the std::map with a custom Allocator (one that kept a
running total of the map's memory usage) should be able to provide at
least an upper bound on how much memory the database entry would need
in order to store all of the map's keys and values.
thnks ..I think this looks better solution
Can you point to some working example of custom allocator , if you
know...
 
J

James Kanze

sizeof() returns the *static* size of an object/type, the value is
determined at compile time and never changes.
You can't do it in any portable way.

You can, sort of. You write a custom allocator which tracks how
much is allocated, and instantiate the map with that.

Whether the information is in anyway useful, of course, is
another issue.

[...]
That doesn't sound like a very good idea. A map is an
associative array, so is a database table. Why not store the
former as the latter?

For example. More significantly, of course, an std::map is
*not* a byte blob, so it can't be stored directly as such. If
for any reason you need to store it as a byte blob in a
database, you'll first have to marshal it.
 
J

Jerry Coffin

well i think the solution to get size is to iterate the map and and
get size of each element
this is the only way i can this right now


basic idea is i need to serialize the map contents so planning to
store as byte blob in db

Knowing how much memory the map occupies won't really do you any good. A
map is (at least normally) implemented as a balanced binary tree that
includes pointers to nodes and balancing information in each node along
with the data you really care about. When you serialize the data, you
just want to store the real data, not the pointers or balancing data.

I'd start by copying the data from the map into a vector:

std::vector<your_map::value_type> data;

std::copy(your_map.begin(), your_map.end(),
std::back_inserter(data));

size_t BLOB_size = sizeof(your_map::value_type) * data.size();

Then to serialize the data, you copy the data directly from the vector
to the database.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top