In an operating system, a Memory Mapping file are virtual storage place which has direct byte to byte correlation between the Virtual Address Space and the corresponding physical storage. So when we access the Virtual Address space via a memory mapping file we are directly communicating with the kernel space where the file is actually loaded. The portion of calculation between the physical storage and logical storage is hence avoided.

Memory Mapping files allows application to access files in the same way as memory. Generally as address between the physical storage and virtual memory address space, we cannot access the physical address directly. But using Memory Mapping Files, the process loads a specific range of address within the process address space with which the storage of memory into the file can be done by just assigning value to a dereferenced pointers. The IO operation on a MemoryMapping file is so fast that from the programmers point of view it seems to be like accessing the memory rather than actual physical storage. To increase performance memory mapping files are not actually stored to the disk file as well, but rather it will be stored automatically in background when FlushViewOfFile is executed or paging file is written. To Read more about memory mapping files, read here.

Memory Mapping file inside .NET Framework

.NET introduces a separate namespace to handle Memory Mapping files. Previously, we needed to do this using unmanaged Api's but with the introduction of managed API into the .NET framework library, it becomes very easy to handle MemoryMapping file directly from .NET library.

As memory mapping files are loaded into memory on a separate range of address space, two process can easily share the same page file instance and thus interprocess communication can be made with fast access to memory. It is recommended to back data with an actual disk file when large data is loaded into memory, so that there is no memory leak on the system when there is large memory pressure.

MemoryMapping with backup Paging file (For large data communication)

We generally use backed up memory file in the hard disk when the memory that needs to be shared between the process goes out of physical memory available on the pc or when you think that the data that needs to be loaded can be very expensive and sensitive to remain only in memory.

The code above gets the reference of MemoryMappedFile using a disk backup paging file. The CreateFromFile static method of MemoryMappedFile exposes an option to specify path of the actual file location where the memory mapping file content needs to be mapped. By opening the file you can load the views of memory mapping file as you are accessing the file content from memory. You can also use OpenExisting or CreateNew methods if you are sure of the specific requirement of usage.

In the above code, you can see I have specified the length of the file as well as a fixed name "INTERPROC" which indicates the name of the shared memory location which other process can call to access. The length of the file needs to be a multiple of 1024 always, hence if you are not using the content of the file, the paging location will still be occupied with null content. This ensures the mapping file always remains in same context as the loaded memory. The file based Memory Mapping files are not loaded totally when you try to access, but rather it provides views in terms of what data you are currently accessing.

Here once the MemoryMappedFile is loaded into memory, you can use CreateViewAccessor to create a separate view to see the content of the data. Here I have loaded the entire data at a time, but it is better to create View content as soon as you want to read its content. Once the data is loaded you can use Read method to get data from the memory.

Almost identical to it, you can use write method to write content into the memory.

Remember
One thing that you need to remember, when you Dispose the memory content from one process that means all the content that are loaded into view needs to be rewritten to the disk file, such that the other process needs to reopen and recreate the view again. If you want to access some specific view without writing to the page file often after using it, tweak the behavior by not disposing the MemoryMappedFile (removing the using block from here) and the operating system will automatically manage the destruction of View.

When the load of memory file is small, it would not be always good to have a large paging file created in background and wasting large amount of memory of the system. Sometimes we need to share content only between the process and when no process is accessing the shared memory, the operating system needs to dispose the content. Regards to this requirement, you can use MemoryMappedFiles to be stored only into memory and be called using the name of Memory created.

In the code above, I have created a MemoryMappedFile using CreateOrOpen and named the actual memory being created as "InterProc". Now this will create a file into the memory without any reference to it into disk file. The HandleInheritability states that the child processes can also access the memory file.

You can also specify security on the memory using MemoryMappedFileSecurity and add Access rule to the security blocks.

You can read from and write to the file in the same way as you did for the file based shared memory, but one thing you need to remember again, you cannot close/dispose the MemoryMappedFile as this will erase all the content that you write into the View.

Here you can see, I didnt use using block or even wrote the Dispose of MemoryMappedFile as it is important.

In the sample application that I have provided with this article, there is an interface that you can use to handle both Large and small Communicators. Ideally these classes needs to be used based on the requirement on real world .

Blog Subscription

Learn MVC 5 step by step

My friend Shivprasad Koirala who is also a Microsoft ASP.NET MVP has released Learn MVC 5 step by step video series. It starts right from basics of MVC and goes to a level until you become a professional.
You can start taking the course for free using the below youtube video.
Please try it, you will find it awesome.

My Awards

Hit Counter

Twitter

Best .NET 4.5 Expert CookBook

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!
Another book on .NET 4.5 has been released very recently. It covers Debugging, Testing, Extensibility, WCF, Windows Phone, Windows Azure and many more...
Grab it now !!!GET ANY BOOK AT $5 from PacktPub. Offer is limited