Windows 7 What's wrong with PAGE_NOCACHE in VirtualAlloc/VirtualFree

erwin

Well-Known Member
Joined
Nov 23, 2009
Messages
2
Hi, I have this code:

Code:
// AllocTest.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include <stdio.h>

int main(int argc, char * argv[])
{    
  if ( ! SetProcessWorkingSetSize( GetCurrentProcess(), 1024 * 1024 * 1024 * 1.2,  1024 * 1024 * 1024 * 1.2 ) ) {
    return -1;
  }

  void * buffer = VirtualAlloc( NULL,  1024 * 1024 * 1024, MEM_COMMIT, PAGE_READWRITE | PAGE_NOCACHE );
  memset( buffer, 1,  1024 * 1024 * 1024 );

  getchar();

  VirtualFree( buffer, 0, MEM_RELEASE );

  return 0;
}
On VirtualFree() there is - with the PAGE_NOCACHE option in VirtualAlloc - a heavy cpu-load over more than 10 sec.
Without this option there is only a short peak in the performance graph. But under XP there is no such behaviour.
 


Solution
The behavior you are observing with heavy CPU load when using the PAGE_NOCACHE option in VirtualAlloc along with VirtualFree is likely due to the caching mechanism in Windows. When you use PAGE_NOCACHE in the VirtualAlloc function, you are essentially specifying that the memory should not be cached. This means that every operation involving this memory region will not be cached by the CPU, leading to more direct and frequent accesses to the physical memory. This behavior can result in increased CPU load, especially during memory operations like freeing the memory with VirtualFree, as the system needs to handle the uncached memory in a different way compared to cached memory regions. The differences...
The behavior you are observing with heavy CPU load when using the PAGE_NOCACHE option in VirtualAlloc along with VirtualFree is likely due to the caching mechanism in Windows. When you use PAGE_NOCACHE in the VirtualAlloc function, you are essentially specifying that the memory should not be cached. This means that every operation involving this memory region will not be cached by the CPU, leading to more direct and frequent accesses to the physical memory. This behavior can result in increased CPU load, especially during memory operations like freeing the memory with VirtualFree, as the system needs to handle the uncached memory in a different way compared to cached memory regions. The differences you are noticing between Windows XP and newer versions could be related to changes in how the operating system handles memory management, caching policies, or optimizations over time. To mitigate the heavy CPU load behavior when using PAGE_NOCACHE, you might consider the following approaches:
  1. Profile and Optimize: Profile your application to understand the impact of using PAGE_NOCACHE and optimize memory allocation and deallocation based on the observed behavior.
  2. Adjust Memory Handling: If possible, optimize memory management in your application to minimize the impact of frequent uncached memory accesses.
  3. Consult Official Documentation: Refer to Microsoft's official documentation and perhaps the WinAPI documentation for any specific guidelines or best practices related to memory management and caching for your scenario. It's essential to consider the implications of adjusting caching behaviors in memory allocation, as it can significantly affect performance, especially in resource-intensive applications.
 


Solution
Back
Top