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

前端 未结 7 685
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  Happy的楠姐
    2020-11-29 03:55

    I recently needed to answer this question for myself, and simply wrote a small benchmark program using std::map I compiled under MSVC 2012 in 64-bit mode.

    A map with 150 million nodes soaked up ~ 15GB, which implies the 8 byte L, 8 byte R, 8 byte int key, and 8 byte datum, totaling 32 bytes, soaked up about 2/3rds of the map's memory for internal nodes, leaving 1/3rd for leaves.

    Personally, I found this to be surprisingly poor memory efficiency, but it is what it is.

    Hope this makes for a handy rule-of-thumb.

    PS: The overhead of a std::map is that of a single node's size AFAICT.

提交回复
热议问题