我该如何衡量共享内存升压进程间向量的大小? [英] How do I measure the size of a boost interprocess vector in shared memory?

查看:159
本文介绍了我该如何衡量共享内存升压进程间向量的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用boost ::进程间:: vector的进程之间共享一些字符串,我想确保我不会溢出它生活在共享内存段。

我如何找到向量占用多少空间在内存中,一个特殊的段分配的字符串将多少内存走?

 的typedef的boost ::进程间:: managed_shared_memory :: segment_manager SegmentManager;
TYPEDEF的boost ::进程间::分配器<焦炭,SegmentManager> CharAllocator;
TYPEDEF的boost ::进程间:: basic_string的<焦炭,的std :: char_traits<焦炭>中CharAllocator> ShmString;
TYPEDEF的boost ::进程间::分配器< ShmString,SegmentManager> StringAllocator;
TYPEDEF的boost ::进程间::矢量< ShmString,StringAllocator> ShmStringVector;常量为size_t SEGMENT_SIZE = ...;addToSharedVector(标准::字符串localString){
    使用空间boost ::进程间;
    managed_shared_memory段(open_only,kSharedMemorySegmentName);
    ShmStringVector * shmvector = segment.find< ShmStringVector>(kSharedMemoryVectorName)。首先,    为size_t currentVectorSizeInShm = ...(shmvector); < -------- HALP!
    为size_t sizeOfNewStringInSharedMemory = ...(localString); < --------    //未示出为清楚起见共享互斥    如果(currentVectorSizeInShm + sizeOfNewStringInSharedMemory< SEGMENT_SIZE){
        CharAllocator charAllocator(segment.get_segment_manager());
        ShmString shmString(charAllocator);
        shmFunctionName = localString.c_str();
        shmvector->的push_back(shmString);
    }
}


解决方案

  1. 快速和肮脏

    您可以共享内存物理映射文件,看看有多少网页实际上已经提交到磁盘。这给你一个粗略的指示,许多实现的网页是在最有可能致力于在1时,和常规内存页面大小是4KB。

    我有另一个答案 [1] 你显示了该方法的基本原理。


  2. 您可以在部门经理使用get_free_memory()。请注意,这并不能说明什么的分配/只/为载体,但它给你多少空间的(可以说是更有用的)想法是的​​实际的占领。


另一个答案 [2] 我已经使用了在内存开销数据容器之间有连续的存储与基于节点的容器基准的差异。

正如你所看到的,个人的分配有很高的开销,和再分配真的很快导致了分裂。所以这是值得看的


  • 保留空间的时间提前至prevent再分配

  • 使用专门的升压进程间分配器,以更好地利用共享内存区域的


[1] 的<一个href=\"http://stackoverflow.com/questions/20991213/memory-mapped-files-managed-mapped-file-and-offset-pointer/21009839#21009839\">Memory映射文件,管理映射文件和偏移指针

[2] 的的坏页头被抛出

I'm using boost::interprocess::vector to share some strings between processes, and I want to make sure I do not overflow the shared memory segment it lives in.

How do I find how much space the vector takes in memory, and how much memory a special segment-allocated string will take?

typedef boost::interprocess::managed_shared_memory::segment_manager SegmentManager;
typedef boost::interprocess::allocator<char, SegmentManager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> ShmString;
typedef boost::interprocess::allocator<ShmString, SegmentManager> StringAllocator;
typedef boost::interprocess::vector<ShmString, StringAllocator> ShmStringVector;

const size_t SEGMENT_SIZE = ...;

addToSharedVector(std::string localString){
    using namespace boost::interprocess;
    managed_shared_memory segment(open_only, kSharedMemorySegmentName);
    ShmStringVector *shmvector = segment.find<ShmStringVector>(kSharedMemoryVectorName).first;

    size_t currentVectorSizeInShm =  ?????(shmvector);            <--------  HALP!
    size_t sizeOfNewStringInSharedMemory =   ?????(localString);  <--------

    //shared mutex not shown for clarity

    if (currentVectorSizeInShm + sizeOfNewStringInSharedMemory < SEGMENT_SIZE)  {
        CharAllocator charAllocator(segment.get_segment_manager());
        ShmString shmString(charAllocator);
        shmFunctionName = localString.c_str();
        shmvector->push_back(shmString);
    }
}

解决方案

  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

这篇关于我该如何衡量共享内存升压进程间向量的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆