How can i estimate memory usage of std::map?

前端 未结 7 667
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 03:06

For example, I have a std::map with known sizeof(A) and sizeof(B), while map has N entries inside. How would you estimate its memory usage? I\'d say it\'s something like

7条回答
  •  时光取名叫无心
    2020-11-29 04:00

    The formula is more like:

    (sizeof(A) + sizeof(B) + factor) * N
    

    where factor is the per entry overhead. C++ maps are typically implemented as red-black trees. These are binary trees, so there will be at least two pointers for the left/right nodes. There will also be some implementation stuff - probably a parent pointer and a "colour" indicator, so factor may be something like

    (sizeof( RBNode *) * 3 + 1) / 2
    

    However, all this is highly implementation dependent - to find out for sure you really need to examine the code for your own library implementation.

提交回复
热议问题