C# Memory Mapped File doesn't take up physical memory space

2k Views Asked by At

I'm trying to cache a large amount of data in physical memory and share them for the other processes in the local environment. So I came up with MMF and read Microsoft MMF document and saw a few examples and started tweaking code like this.

MemoryMappedFile MMF = MemoryMappedFile.CreateNew("Features", MaxSize);
.
.
.
using (MemoryMappedViewStream stream = MMF.CreateViewStream())
{
     BinaryWriter write = new BinaryWriter(stream);
     .
     .
     .
     // Fetched the data from database and store it into Rowdata variable.
     while (Rowdata.Read())
     {
          byte[] data = Rowdata.GetFieldValue<byte[]>(0);
          write.Write(data);
     }
}

It doesn't hold the data in the memory

IVSSharedMemory is the one that I'm working on, but the memory size is supposed to be much higher than that.

enter image description here

Is there something I forgot to do when I created memory mapped file? please tell me if there is. I googled this right after I noticed this unexpected behavior and some said that it's a virtual memory. But I cannot help thinking it's not true because, in the document, it explains this in the section below.

Non-Persisted Memory-Mapped Files

The CreateNew and CreateOrOpen methods create a memory-mapped file that is not mapped to an existing file on disk.

Thanks in advance. Just confirming that it's what MMF designed for would be appreciated too.

UPDATE


It is indeed a virtual memory it seems. As Mike z commented, I inspected the memories that my app was holding in VMMAP tool and the result was just what I wanted to see.

enter image description here

But take a look at this, the amount of committed memory has changed. This happens when my app loads all the data completely. I can guess that the previous size just indicates the MaxSize that I assigned when I create MMF.

enter image description here

Is it gone to a physical disk space or something?

I read a lot about MemoryMappedFile and trying to see what's beneath underground. But I still don't get why this is happening. Seriously, where is the data located? where can I inspect it on?

1

There are 1 best solutions below

0
On

A memory mapped file is a functionality provided by the kernel of most modern OS. Its intent is not to load the file in actual physical memory but instead to leverage the virtual memory mapping functionality so as to provide access to the content of a file as if it was a block of memory.

To be more precise, modern OS's use a pagination mechanism that uses the MMU ship of the computer to map a virtual address space to either physical memory or to a swap file on disk. This is how virtual memory is implemented. The idea is that the MMU translates memory access from the CPU to the RAM using a mapping maintained by the kernel, or if the page is missing (either it has been swapped to disk or is not mapped at all) then produces a hardware interrupt that will call the kernel to resolve the cache miss. In the case of a memory mapped file, this resolution takes the form of loading the missing page into main memory and resuming execution of the program transparently.