Given the following test code (x64 environment):
static void Test() { string fileName = @"d:\map"; long length = new FileInfo(fileName).Length; using (var file = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, "mapFile", length, MemoryMappedFileAccess.ReadWrite)) { byte* bytePtr = (byte*)0; var view = file.CreateViewAccessor(0, length, MemoryMappedFileAccess.ReadWrite); view.SafeMemoryMappedViewHandle.AcquirePointer(ref bytePtr); long count = (long)(length / sizeof(int)); long sum = 0; long step = count / 2000; int* ptr = (int*)&bytePtr[0]; long currentCount = 0 ; Parallel.For(0, count, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (i) => { Interlocked.Add(ref sum, ptr[i]); Interlocked.Increment(ref currentCount) ; if (currentCount % step == 0) Console.Write("\r{0:0.00}%", (100 * currentCount / (float)count)); }); Console.WriteLine(sum); view.Dispose(); } }
Given that "d: \ map" is a 40-gigabyte file, there is a very strange behavior with random access via the "ptr" pointer.
The physical memory of the system is fully used, and everything slows down, and this process takes more than 2 hours.
When I have serial (and single-threaded) access, the physical memory used does not exceed 1 GB and the process takes about 8 minutes.
My question is: when using a file with memory mapping, is "real" memory used? Not just a busy virtual address space?
I am trying to understand the physical memory consumption when using a memory mapped file.
source share