On a 32-bit and 64-bit Windows machine, I have to allocate memory for storing large amounts of data that are broadcast in real time, for a total of about 1 GB. If I use malloc (), I am going to get the virtual memory address, and this address can actually cause some paging to the hard disk depending on the amount of memory that I have. Unfortunately, I'm afraid that HD will affect performance and result in a lack of data.
Is there a way to make the memory allocate only in RAM, even if it means that I get an error when there is not enough memory (so the user needs to close other things or use another machine)? I want to guarantee that all operations will be performed in memory. If this fails, forcing the removal of the application is acceptable.
I know that another process may come and take some memory by itself, but I'm not worried because this does not happen on this computer (this will be the only application on the machine that will make this big allocation).
[Edit:] My attempt so far has been to try using VirtualLock as follows:
if(!SetProcessWorkingSetSize(this, 300000, 300008)) printf("Error Changing Working Set Size\n"); // Allocate 1GB space unsigned long sz = sizeof(unsigned char)*1000000000; unsigned char * m_buffer = (unsigned char *) malloc(sz); if(m_buffer == NULL) { printf("Memory Allocation failed\n"); } else { // Protect memory from being swapped if(!VirtualLock(m_buffer , sz)) { printf("Memory swap protection failed\n"); } }
But a failure in the working set fails, as well as VirtualLock. Malloc returns nonzero.
[Edit2] I also tried:
unsigned long sz = sizeof(unsigned char)*1000000000; LPVOID lpvResult; lpvResult = VirtualAlloc(NULL,sz, MEM_PHYSICAL|MEM_RESERVE, PAGE_NOCACHE);
But lpvResult is 0, so you're out of luck too.
Gustavo litovsky
source share