Boost logo

Boost :

Subject: [boost] [interprocess] How to know how much memory is taken up by an object allocated in boost interprocess shared memory?
From: nav999 (nkipe_at_[hidden])
Date: 2015-10-07 23:30:19


Re-posting, as the boost mailing list rejected the first post
<http://boost.2283326.n4.nabble.com/How-to-know-how-much-memory-is-taken-up-by-an-object-allocated-in-boost-interprocess-shared-memory-tc4680682.html>
mentioning that I had to add "[interprocess]" to the subject line.
I have asked this question on stackoverflow
<http://stackoverflow.com/questions/30642585/why-does-an-object-allocated-in-boost-interprocess-shared-memory-take-up-more-me>
, but got no response.

For this program:
#include <iostream>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <iostream>

#define SHARED_MEMORY_NAME "SO12439099-MySharedMemory"
#define DATAOUTPUT "OutputFromObject"
#define INITIAL_MEM 650000
#define STATE_MATRIX_SIZE 4

using namespace std;
namespace bip = boost::interprocess;

class SharedObject
{
public:
    unsigned int tNumber;
    bool pRcvdFlag;
    bool sRcvdFlag;
    unsigned long lTimeStamp;
};

typedef bip::allocator<SharedObject,
bip::managed_shared_memory::segment_manager> ShmemAllocator;
typedef bip::list<SharedObject, ShmemAllocator> SharedMemData;

int main()
{
        bip::managed_shared_memory* seg;
        SharedMemData *sharedMemOutputList;

        bip::shared_memory_object::remove(DATAOUTPUT);
        seg = new bip::managed_shared_memory(bip::create_only, DATAOUTPUT,
INITIAL_MEM);
        const ShmemAllocator alloc_inst(seg->get_segment_manager());
        sharedMemOutputList =
seg->construct<SharedMemData>("TrackOutput")(alloc_inst);

        std::size_t beforeAllocation = seg->get_free_memory();
        std::cout<<"\nBefore allocation = "<< beforeAllocation <<"\n";
        SharedObject temp;
        sharedMemOutputList->push_back(temp);
        std::size_t afterAllocation = seg->get_free_memory();
        std::cout<<"After allocation = "<< afterAllocation <<"\n";
        std::cout<<"Difference = "<< beforeAllocation - afterAllocation
<<"\n";
        std::cout<<"Size of SharedObject = "<< sizeof(SharedObject) <<"\n";
        std::cout<<"Size of SharedObject's temp instance = "<< sizeof(temp)
<<"\n";

}//main

The output is:

Before allocation = 649680
After allocation = 649632
Difference = 48
Size of SharedObject = 16
Size of SharedObject's temp instance = 16

If the size of SharedObject and it's instance is 16 bytes, then how can the
difference in allocation be 48? Even if padding had automatically been done,
it's still too much to account for 3 times the size (for larger structures
it goes to 1.33 times the size).
Because of this, I'm unable to allocate and dynamically grow the shared
memory reliably. If SharedObject contains a list which grows dynamically,
that could add to the uncertainty of space allocation even more.

How can these situations be safely handled? How can I know exactly how much
of memory to allocate?

--
View this message in context: http://boost.2283326.n4.nabble.com/interprocess-How-to-know-how-much-memory-is-taken-up-by-an-object-allocated-in-boost-interprocess-sh-tp4680745.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk