Memory consumption by STL containers

前端 未结 2 2046
醉酒成梦
醉酒成梦 2020-12-06 06:06

I am working on an application in which I am planning to use couple of STL containers. The application will take certain steps if memory consumption reaches a threshold. For

2条回答
  •  没有蜡笔的小新
    2020-12-06 07:06

    A std::vector typically takes 3 machine words in total + sizeof(element) * capacity() of memory. For typical implementations, the overhead consist of pointers to the beginning, end and current size of the vector. The elements themselves are stored in contiguous memory. capacity() typically has room for up to twice the actual number of elements.

    A std::map typically takes about 2 machine words in total + 3 machine words per element + [ sizeof(element) +sizeof(int) ] * num_elements of memory. For typical implementations, the overhead consists of pointers to the stored elements. The elements themselves are stored in a binary tree, with pointers to its parent and two children.

    With these rules of thumb, all you need to know is the average number of characters per string and the total number of strings to know total memory consumption.

提交回复
热议问题