Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 204
hina10531 Avatar asked Jun 14 '17 07:06

hina10531


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

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 from of loading the missing page into main memory and resuming execution of the program transparently.

like image 78
Samuel Vidal Avatar answered Sep 22 '22 12:09

Samuel Vidal