How do I measure the size of a boost interprocess vector in shared memory?

被刻印的时光 ゝ 提交于 2019-11-30 21:05:09
sehe
  1. Quick and dirty

    You can make the shared memory a physically mapped file and see how many pages have actually been committed to disk. This gives you a rough indication on many implementations as pages are most likely committed 1 at at time, and usual memory pages sizes are 4kb.

    I have another answer[1] that shows you the basics of this method.

  2. You can use the get_free_memory() on the segment manager. Note that this doesn't say what's allocated /just/ for that vector, but it gives you an (arguably more useful) idea of how much space is actually occupied.

In another answer [2] I have used that to benchmark differences in memory overhead between data containers with contiguous storage vs. node-based containers.

As you can see, individual allocations have high overhead, and reallocation leads to fragmentation really quickly. So it's worth looking at

  • reserving space ahead of time to prevent reallocations
  • using specialized Boost Interprocess allocators to make better use of the Shared Memory area

[1]see Memory Mapped Files, Managed Mapped File and Offset Pointer

[2] see Bad alloc is thrown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!