473,503 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my code to prove working set larger than virtual bytes

200 New Member
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 1573

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

Similar topics

4
21589
by: Mike | last post by:
hopefully this is an easy one to solve and I'm just missing something little... basically, I had a nice little website running with php, apache, etc... that uses imagecreatefromjpeg to create...
2
3759
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
171
7584
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
5
1867
by: Steven T. Hatton | last post by:
I'm engaged in a discussion on the Qt mailing list regarding exceptions. It has been stated that exception handling leads to significantly larger executable images than does 'error-code'...
1
1857
by: anonymous | last post by:
I have written a simple code using inline assembly to compare the individual bytes of two lists (X and Y) and to store the higher byte of each list in a third list (LARGER). The code is as follows...
1
2206
by: George2 | last post by:
Hello everyone, I am using Windows Server 2003 Performance Counter tool to monitor the memory consumed by my process. The interested terms are working set, virtual bytes and private bytes. My...
0
1298
by: George2 | last post by:
Hello everyone, I am using perfmon to watch the working set and virtual bytes, when I do a keyword search in SourceInsight. I found the value of working set is larger than virtual bytes when...
0
1622
by: George2 | last post by:
Hello everyone, Sorry that this question is related to another question I posted some time before because I have some new findings and self-analysis. My question is why sometimes from perfmon...
1
2120
by: George2 | last post by:
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...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.