Search code examples
c++qtmemory64-bitmingw32

Allocate large memory using x64 system with x86 application in c++


I have a problem and couldn't find a solution (because of the lack of knowledge).

The problem is that my application (written in c++ compiled with mingw x86 using Qt 5.3.2) allocates blocks of memory which in summary should accommodate from 7.5 to 8.5 GB (I have precomputed this using calc). Each block is 1 MB.

Application allocates memory in a cycle, one iteration allocates 1 MB of memory.
I have only 8 GB of memory but when I use monitors to see when it stops allocating I see that it doesn't depend how much memory is accomodated at the moment of starting the app.

It always stops allocating when the app allocates 4 GB and it doesn't depend on the fact that there is still 2 free GB in the system (before start I have nearly 6 GB free of 8) or there isn't.

So I can't understand why it happens in such way but I think that it is because of x86 structure of the app.

But I need to use the app on x86 systems too (I know that there can't be such x86 system with more than 4 GB).

How can I solve the problem?

I prefere ways that would save the x86 structure of the app and allow it to allocate more than 4 GB.
But if there is no such solution I would be made to use x64 structure of the app.


Solution

  • The reason your application stops filling memory after 4 GB is because it is using 32-bit pointers. With 32 bits, you can only represent 2^32, or 4,294,967,296 bytes of memory. Physically speaking, you cannot fill up more space.

    More specifically, your program can also be given maximum 4 GB of pages from the OS, so using 2 32-bit pointers won't help.

    Unfortunately this means that you have to make the switch to 64-bit, which would allow a lot more memory locations to be addressed.