Memory occupied by Map

Discussion in 'C++' started by alien.0101@gmail.com, Jun 13, 2008.

  1. Guest

    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???
     
    , Jun 13, 2008
    #1
    1. Advertising

  2. Guest

    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
     
    , Jun 13, 2008
    #2
    1. Advertising

  3. Greg Herlihy Guest

    On Jun 12, 6:01 pm, "Daniel T." <> wrote:
    > "" <> 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
     
    Greg Herlihy, Jun 13, 2008
    #3
  4. Guest

    On Jun 12, 11:47 pm, Greg Herlihy <> wrote:
    > On Jun 12, 6:01 pm, "Daniel T." <> wrote:
    >
    > > "" <> 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
     
    , Jun 13, 2008
    #4
  5. James Kanze Guest

    On Jun 13, 3:01 am, "Daniel T." <> wrote:
    > "" <> 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:
    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 13, 2008
    #5
  6. Jerry Coffin Guest

    In article <e23aebaa-b2d6-4df6-93a8-1b2e9f5933c3
    @p25g2000hsf.googlegroups.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.
     
    Jerry Coffin, Jun 15, 2008
    #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. alex
    Replies:
    1
    Views:
    692
    Lau Lei Cheong
    Feb 4, 2005
  2. Replies:
    2
    Views:
    403
    Mark Space
    Jul 7, 2006
  3. Ravi

    Show all memory occupied by the program

    Ravi, Jun 3, 2007, in forum: C Programming
    Replies:
    31
    Views:
    832
    Keith Thompson
    Jun 6, 2007
  4. Lambda
    Replies:
    2
    Views:
    2,802
    James Kanze
    May 20, 2008
  5. swapnalimore
    Replies:
    0
    Views:
    717
    swapnalimore
    Jul 12, 2011
Loading...

Share This Page