470,815 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

my code to prove working set larger than virtual bytes

200 100+
Hello everyone,


From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than virtual memory (subset relationship).

But the following simple code on Windows Server 2003 proves (if you monitor virtual bytes counter and working set bytes conuter from perfmon), if we do not unmap the page map file, the working set will continue to increase (and much larger than virtual bytes) until we unmap it.

Take a breakpoint before following code section,

Expand|Select|Wrap|Line Numbers
  1.             // close mapped files to avoid leak
  2.             for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex++)
  3.             {
  4.                 if (map [sectionIndex])
  5.                 { 
  6.                     UnmapViewOfFile(map [sectionIndex]);
  7.                 }
  8.             }
  9.  
Any ideas? Does my code break the definition of working set? Why working set is much larger than virtual bytes?

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h> 
  2. #include <stdio.h> 
  3.  
  4. int main(int argc, char* argv[]) 
  5.     LARGE_INTEGER start,end; 
  6.     LARGE_INTEGER freq; 
  7.     QueryPerformanceCounter(&start); 
  8.     QueryPerformanceFrequency(&freq); 
  9.  
  10.     MEMORYSTATUS memstat; 
  11.     void** map;
  12.     int sectionIndex = 0;
  13.     memstat.dwLength = sizeof(memstat); 
  14.     GlobalMemoryStatus(&memstat); 
  15.  
  16.     // basic file mapping test (512 MB) 
  17.     long long size = 512*1024*1024; 
  18.  
  19.     HANDLE mapping = 
  20.     CreateFileMapping(NULL,NULL,PAGE_READWRITE|SEC_COMMIT,(DWORD)(size>>32),DWORD(size),NULL); 
  21.     if (mapping) 
  22.     { 
  23.         // create and destroy temporary views 
  24.         SYSTEM_INFO sysInfo; 
  25.         GetSystemInfo(&sysInfo); 
  26.         const int allocSize = sysInfo.dwAllocationGranularity; 
  27.  
  28.         GlobalMemoryStatus(&memstat); 
  29.  
  30.         void *mem = new char[allocSize]; 
  31.         memset(mem,0x11,allocSize); 
  32.  
  33.         map = (void**) new char [sizeof(void*) * size / allocSize];
  34.  
  35.         for (int i=0; i<10; i++) 
  36.         { 
  37.  
  38.             sectionIndex = 0;
  39.             for (long long offset=0; offset<=size-allocSize; offset+=allocSize) 
  40.             { 
  41.                 map [sectionIndex] = 
  42.                 MapViewOfFile(mapping,FILE_MAP_WRITE,(DWORD)(offset>>32),(DWORD)offset,allocSize); 
  43.                 if (map [sectionIndex]) 
  44.                 { 
  45.                     memcpy(map [sectionIndex],mem,allocSize); 
  46.                     // UnmapViewOfFile(map);
  47.                 }
  48.  
  49.                 sectionIndex++;
  50.             } // for (long long offset=0; offset<=size-allocSize; offset+=allocSize) 
  51.  
  52.             // close mapped files to avoid leak
  53.             for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex++)
  54.             {
  55.                 if (map [sectionIndex])
  56.                 { 
  57.                     UnmapViewOfFile(map [sectionIndex]);
  58.                 }
  59.             }
  60.  
  61.             GlobalMemoryStatus(&memstat); 
  62.  
  63.             sectionIndex = 0;
  64.             for (long long offset=0; offset<=size-allocSize; offset+=allocSize) 
  65.             { 
  66.                 map [sectionIndex] = 
  67.                 MapViewOfFile(mapping,FILE_MAP_READ,(DWORD)(offset>>32),(DWORD)offset,allocSize); 
  68.                 if (map [sectionIndex]) 
  69.                 { 
  70.                     for (int t=0; t<allocSize; t++) 
  71.                     { 
  72.                         if (((char *)(map [sectionIndex]))[t]!=0x11) 
  73.                         { 
  74.                             OutputDebugString("Memory read failed\n"); 
  75.                         } 
  76.                     } 
  77.                 } 
  78.  
  79.                 UnmapViewOfFile(map [sectionIndex]);
  80.             } 
  81.  
  82.             // close mapped files to avoid leak
  83.             /*
  84.             for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex++)
  85.             {
  86.                 if (map [sectionIndex])
  87.                 { 
  88.                     UnmapViewOfFile(map [sectionIndex]);
  89.                 }
  90.             }
  91.             */
  92.  
  93.             GlobalMemoryStatus(&memstat); 
  94.         } // for (int i=0; i<10; i++)
  95.  
  96.         QueryPerformanceCounter(&end); 
  97.  
  98.         GlobalMemoryStatus(&memstat); 
  99.  
  100.         printf("Time %.3f\n", 
  101.         double(end.QuadPart-start.QuadPart)/double(freq.QuadPart)); 
  102.         CloseHandle(mapping); 
  103.         delete[] mem; 
  104.         GlobalMemoryStatus(&memstat); 
  105.     } //if (mapping)
  106.  
  107.     return 0;
  108.  

thanks in advance,
George
Jan 17 '08 #1
0 1408

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Mike | last post: by
2 posts views Thread by Bryan Olson | last post: by
171 posts views Thread by tshad | last post: by
5 posts views Thread by Steven T. Hatton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.