[interprocess] Determine size of managed_shared_memory

Hi all, I want to create a managed_shared_memory in exactly the size I needed. Therefore I do something like this: size = sizeof( common_managed_shared_memory ); size += sizeof( MyClassInSharedMem ); managed_shared_memory segment(create_only, "name", size ); MyClassInSharedMem allocates itself some memory by segment->allocate(). I added this also to the size, but get a boost::interprocess::bad_alloc exception on allocating this additional memory. Are there some magic bytes I forgot to add? Many thanks in advance Kai

On 05/08/2010 16:39, Kai Benndorf wrote:
Hi all,
I want to create a managed_shared_memory in exactly the size I needed. Therefore I do something like this:
size = sizeof( common_managed_shared_memory ); size += sizeof( MyClassInSharedMem );
managed_shared_memory segment(create_only, "name", size );
MyClassInSharedMem allocates itself some memory by segment->allocate(). I added this also to the size, but get a boost::interprocess::bad_alloc exception on allocating this additional memory.
Are there some magic bytes I forgot to add?
Yes, but you can't calculate it, because there are memory allocation bookeeping and fragmentation issues that change in runtime depending on your allocation/deallocation pattern. And shared memory is allocated by pages by the OS (4K on linux 64k on windows), so any allocation will be in practice allocated rounded to a page: managed_shared_memory segment(create_only, "name", 20); will waste the same memory as: managed_shared_memory segment(create_only, "name", 4096); Ion

Hi, I want to continue on this topic. when i have created the maximum number of objects needed in the application in shmem, the consumed memory is n bytes.(managed_shm.get_size() - managed_shm.get_free_memory();) However, during run time these objects can be deleted and created multiple times causing fragmentation. Is there a recommended size that can safe against bad_allocs due to fragmentation (like 2n, 3n)? -Vivek -- Sent from: http://boost.2283326.n4.nabble.com/Boost-Users-f2553780.html
participants (3)
-
Ion Gaztañaga
-
Kai Benndorf
-
vivek1043