[Interprocess] Minimum allocation size for managed_shared_memorty
I have a question about Boost.Interprocess library. I am sharing a fixed
size boost::interprocess::vector using managed_shared_memory. The vector
length is 100 and the type of element in the vector is
boost::array
On 29/11/2010 6:01, Aditya Gadre wrote:
I have a question about Boost.Interprocess library. I am sharing a fixed size boost::interprocess::vector using managed_shared_memory. The vector length is 100 and the type of element in the vector is boost::array
. That makes (vector::size)*sizeof(boost::array = 100*8 = 800. . If I create a managed_shared_memory segment with length 800 bytes and create a vector in that segment, then boost::interprocess::bad_alloc is thrown. If however I create a segment with a length of say 8000 and then create the vector, then everything works well. So my question is what is the minimum size required for a managed_shared_memory segment apart from what the size of a vector?
There is a fixed overhead for the header and another one for each allocation (bookeeping information for deallocation), another overhead for each named object (you need to store the name, and other information...). You can also have memory fragmentation (if you allocate/deallocate many objects), just like with heap memory and this overhead is use-dependent. Take in care that the OS will reserve shared memory in page units (usually 4KB-64KB bytes, depending on HW/OS) so creating a segment of 800 will waste a whole page. See mapped_region::get_page_size() to know the size of a page in your platform. Best, Ion
There is a fixed overhead for the header and another one for each allocation (bookeeping information for deallocation), another overhead for each named object (you need to store the name, and other information...). You can also have memory fragmentation (if you allocate/deallocate many objects), just like with heap memory and this overhead is use-dependent.
Take in care that the OS will reserve shared memory in page units (usually 4KB-64KB bytes, depending on HW/OS) so creating a segment of 800 will waste a whole page. See mapped_region::get_page_size() to know the size of a page in your platform.
On my machine, the page size is 4096 bytes. In general, is it advisable to allocate memory in multiples of the page size? Thanks, - Aditya
On 29/11/2010 17:08, Aditya Gadre wrote:
On my machine, the page size is 4096 bytes. In general, is it advisable to allocate memory in multiples of the page size? Thanks,
We'll, if you need fixed allocation it does not matter. If you are going to use dynamic allocation functions, if you don't allocate in multiples of the page-size, you are going to waste memory, since no one can use the the portions of the pagesize you don't fully map. See: http://www.boost.org/doc/libs/1_45_0/doc/html/interprocess/sharedmemorybetwe... Best, Ion
participants (2)
-
Aditya Gadre
-
Ion Gaztañaga