My system: Physical memory: 3gb
Windows XP Service Pack 3 (32 bit) Paging file size: 30gb
Purpose: to find the maximum possible size of a memory card that I can allocate on my machine.
When I run the following code to place a 2gb memory card file, the call fails.
handle = CreateFileMapping (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, INT_MAX, NULL);
I was very puzzled by this, because I can allocate a memory card file up to the size of a 30gb system page file, constantly calling CreateFileMapping with 100mb at a time.
After restarting the computer and restarting the application, which requests a 2gb file with memory mapping in CreateFileMapping, it works and it returns a valid handle. So it bothers me a bit, what the hell is going on under the hood with windows?
So, the situation is this, I can create many small files with memory mapping using the entire system page file (30gb), but when I request one 2gb allocation, the call fails. When you restart the machine and start the same application, the call will be successful!
Some notes:
1) A file with memory mapping is not loaded into the proccess virtual address space, the file is not yet displayed.
2) The OS can allocate small memory files of 100 MB per 30 gb of system page file!
At the moment, I have come to the conclusion that the Windows XP SP3 virtual memory manager (32-bit) cannot successfully reserve the requested 2gb in the system page file, and then crash due to fragmentation of the system memory (it looks like it needs to reserve the continuation of memory allocation, even if the page file is 4kb each). After the reboot, I assume that the fragmentation of the system memory is less, which allows the same call to succeed and allocate a file with a 2 GB memory map.
I conducted several experiments, after starting the machine during the day, I launched a small application that would allocate a file with 300 MB memory and then released it. Then it will increase the size by 1 mb and try again. Finally, he stops at around 700 MB and reports (insufficient system resources). Then I will go through and close each application, and this, in turn, will stop error messages and finally continue to allocate a file with a memory display of 3.5 GB in size!
So my question is: what is going on here? There must be some type of memory fragmentation between the virtual memory manager, since allocating files with 100 MB of memory will consume up to 30 GB of the system page file (commit restriction).
Update
The conclusion is that if you are going to create a large file with memory mapping supported by the system page file with INVALID_HANDLE_VALUE, then the system page file (swap file) must be resized to the required size and be in a fragmented state for large distributions> 2gb ! Although with a heavy load of IO, it can still fail. To get around all these problems, you can create your own file with the required size (I made 1tb) and a memory card for this file.
Final update
I ran the same tests in a Windows 7 window, and, to my surprise, it works every time (up to the size of the system page file) without touching anything. Therefore, I think this is just a mistake, that large memory allocations can fail more often in Windows XP than in Windows 7.