Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Memory occupied by Map (http://www.velocityreviews.com/forums/t619951-memory-occupied-by-map.html)

alien.0101@gmail.com 06-13-2008 12:14 AM

Memory occupied by Map
 
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???

alien.0101@gmail.com 06-13-2008 01:20 AM

Re: Memory occupied by Map
 
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

Greg Herlihy 06-13-2008 06:47 AM

Re: Memory occupied by Map
 
On Jun 12, 6:01*pm, "Daniel T." <danie...@earthlink.net> wrote:
> "alien.0...@gmail.com" <alien.0...@gmail.com> wrote:
>
> > How can I find how much bytes does the map uses.

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



alien.0101@gmail.com 06-13-2008 08:36 AM

Re: Memory occupied by Map
 
On Jun 12, 11:47*pm, Greg Herlihy <gre...@mac.com> wrote:
> On Jun 12, 6:01*pm, "Daniel T." <danie...@earthlink.net> wrote:
>
> > "alien.0...@gmail.com" <alien.0...@gmail.com> wrote:

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

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

thnks ..I think this looks better solution
Can you point to some working example of custom allocator , if you
know...

>
> Greg



James Kanze 06-13-2008 12:01 PM

Re: Memory occupied by Map
 
On Jun 13, 3:01 am, "Daniel T." <danie...@earthlink.net> wrote:
> "alien.0...@gmail.com" <alien.0...@gmail.com> wrote:
> > I have written a sample application and adding value to map doesnot
> > change size


> sizeof() returns the *static* size of an object/type, the value is
> determined at compile time and never changes.


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


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

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


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

--
James Kanze (GABI Software) email:james.kanze@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

Jerry Coffin 06-15-2008 02:39 PM

Re: Memory occupied by Map
 
In article <e23aebaa-b2d6-4df6-93a8-1b2e9f5933c3
@p25g2000hsf.googlegroups.com>, alien.0101@gmail.com says...
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.


All times are GMT. The time now is 10:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.