/* * You can explore how reserved and committed memory appear in TaskManager, PerfMon and so on * with the following small program. This uses the Windows VirtualAlloc call to first reserve, * and then commit memory. Finally it starts to touch the committed memory, writing a value * into it every 4096 bytes, that is, into every memory page. This brings the pages into the * working set. If you run this program and watch it with Task Manager or PerfMon, * you will see how the committed and reserved memory belonging to the process changes. * For example when this program commits memory you will see the Task Manager VMSize value go up; * as it touches memory you will see the Task Manager MemUsage value go up. * * This program has been compiled with Microsoft Visual Studio's C compiler as follows: * cl experiment.c * This generates experiment.exe. */ #include #include #define MB_TO_RESERVE 200 /* reserving 200 MB */ #define MB_TO_COMMIT 100 /* committing 100 MB */ #define MB_TO_TOUCH 30 /* touching 30 MB */ #define PAGE_SIZE 4096 /* page size is almost always 4K */ /* could use GetSystemInfo to check */ void main () { void * reserved_p; /* the starting address of the block we shall reserve */ void * committed_p; /* the starting address of the block we shall commit */ int mb; /* will be used to iterate through memory */ /**************************************************************************** * reserve a stretch of the address space ****************************************************************************/ reserved_p = VirtualAlloc(NULL, MB_TO_RESERVE*1024*1024, MEM_RESERVE, PAGE_READWRITE); printf("reserved a stretch of the address space...press enter to commit...\n"); getchar(); /**************************************************************************** * commit part of the reserved address space ****************************************************************************/ committed_p = VirtualAlloc(reserved_p, MB_TO_COMMIT*1024*1024, MEM_COMMIT, PAGE_READWRITE); printf("committed part of the reserved address space...press enter to touch...\n"); getchar(); /***************************************************************************** * gradually touch more and more of the committed memory *****************************************************************************/ printf("gradually touching more and more of the committed memory...\n"); for (mb = 1; mb < MB_TO_TOUCH ; mb++) { char * p; // used to iterate down committed pages, touching them all printf("...touching %d MB...\n",mb); /*************************************************************************** * march down memory touching every page ***************************************************************************/ for (p = committed_p; p < (char*)committed_p + mb * 1024 * 1024; p += PAGE_SIZE) { *p = 'x'; } Sleep(1000); } printf("...finished\n"); }