qtqsharedmemory

Why QShared Memory create size and returned size() are different


I have a doubt in QSharedMemory

if i create a shared memory & it size is less than 4096 the size() function returned 4096.

If the created size is greater than 4096, then it return 4096+created size.

Eg:

QSharedMemory mem("MyApp");
mem.create(1);
qDebug("Size=%d",mem.size());//4096

QSharedMemory mem("MyApp");
mem.create(4095);
qDebug("Size=%d",mem.size());//4096

QSharedMemory mem("MyApp");
mem.create(4097);
qDebug("Size=%d",mem.size());//8192

How to get the correct size?

I am using Windows 7 32-bit OS


Solution

  • There is nothing wrong with QSharedMemory. It shows you the real physical memory usage, which is not what we are use to with virtual memory.

    In practice, physical memory granularity is a page, which has several bytes. Usually 4096 bytes. When you allocate one byte, It consume a whole physical page.

    When a process deal with memory he is dealing virtual memory, which provide powerful tools. For instance virtual memory manager can use the same physical page for several one byte allocations. But virtual memory is only relevant at the process scope.

    Here you have memory shared by several processes, so it is a different memory model. The Qt devs just made the design decision to make this reality visible to the user of the framework.