Memory Mapped File Windows
A memory-mapped file contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory. Starting with the .NET Framework 4, you can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files.
On Memory-Mapped Files. When you map a file to memory, you instruct the Windows memory manager to create a data structure that maps a region of virtual memory pages in your process’ address. Here you can see file c3725-adventerprisek9-mz.124-21a.bin-127.0.0.1.ghost is memory mapped in the user virtual memory range 0x69a50000 - 0x71a4f000 (131KB) Memory mapping attribute protection is EXECUTEWRITECOPY. However lkd show a commit charge of 262144 KB (65536 pages of 4KB each).
There are two types of memory-mapped files:
Persisted memory-mapped files
Persisted files are memory-mapped files that are associated with a source file on a disk. When the last process has finished working with the file, the data is saved to the source file on the disk. These memory-mapped files are suitable for working with extremely large source files.
Non-persisted memory-mapped files
Non-persisted files are memory-mapped files that are not associated with a file on a disk. When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. These files are suitable for creating shared memory for inter-process communications (IPC).
Processes, Views, and Managing Memory
Memory-mapped files can be shared across multiple processes. Processes can map to the same memory-mapped file by using a common name that is assigned by the process that created the file.
Windows Memory Mapped File Size
To work with a memory-mapped file, you must create a view of the entire memory-mapped file or a part of it. You can also create multiple views to the same part of the memory-mapped file, thereby creating concurrent memory. For two views to remain concurrent, they have to be created from the same memory-mapped file.
Multiple views may also be necessary if the file is greater than the size of the application’s logical memory space available for memory mapping (2 GB on a 32-bit computer).
There are two types of views: stream access view and random access view. Use stream access views for sequential access to a file; this is recommended for non-persisted files and IPC. Random access views are preferred for working with persisted files.
Memory-mapped files are accessed through the operating system’s memory manager, so the file is automatically partitioned into a number of pages and accessed as needed. You do not have to handle the memory management yourself.
The following illustration shows how multiple processes can have multiple and overlapping views to the same memory-mapped file at the same time.
The following image shows multiple and overlapped views to a memory-mapped file:
Programming with Memory-Mapped Files
The following table provides a guide for using memory-mapped file objects and their members.
Task | Methods or properties to use |
---|---|
To obtain a MemoryMappedFile object that represents a persisted memory-mapped file from a file on disk. | MemoryMappedFile.CreateFromFile method. |
To obtain a MemoryMappedFile object that represents a non-persisted memory-mapped file (not associated with a file on disk). | MemoryMappedFile.CreateNew method. - or - MemoryMappedFile.CreateOrOpen method. |
To obtain a MemoryMappedFile object of an existing memory-mapped file (either persisted or non-persisted). | MemoryMappedFile.OpenExisting method. |
To obtain a UnmanagedMemoryStream object for a sequentially accessed view to the memory-mapped file. | MemoryMappedFile.CreateViewStream method. |
To obtain a UnmanagedMemoryAccessor object for a random access view to a memory-mapped fie. | MemoryMappedFile.CreateViewAccessor method. |
To obtain a SafeMemoryMappedViewHandle object to use with unmanaged code. | MemoryMappedFile.SafeMemoryMappedFileHandle property. - or - MemoryMappedViewAccessor.SafeMemoryMappedViewHandle property. - or - MemoryMappedViewStream.SafeMemoryMappedViewHandle property. |
To delay allocating memory until a view is created (non-persisted files only). (To determine the current system page size, use the Environment.SystemPageSize property.) | CreateNew method with the MemoryMappedFileOptions.DelayAllocatePages value. - or - CreateOrOpen methods that have a MemoryMappedFileOptions enumeration as a parameter. |
Security
You can apply access rights when you create a memory-mapped file, by using the following methods that take a MemoryMappedFileAccess enumeration as a parameter:
Memory Mapped File Windows C++
You can specify access rights for opening an existing memory-mapped file by using the OpenExisting methods that take an MemoryMappedFileRights as a parameter.
In addition, you can include a MemoryMappedFileSecurity object that contains predefined access rules.
To apply new or changed access rules to a memory-mapped file, use the SetAccessControl method. To retrieve access or audit rules from an existing file, use the GetAccessControl method.
Examples
Persisted Memory-Mapped Files
The CreateFromFile methods create a memory-mapped file from an existing file on disk.
The following example creates a memory-mapped view of a part of an extremely large file and manipulates a portion of it.
The following example opens the same memory-mapped file for another process.
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.
The following example consists of three separate processes (console applications) that write Boolean values to a memory-mapped file. The following sequence of actions occur:
Process A
creates the memory-mapped file and writes a value to it.Process B
opens the memory-mapped file and writes a value to it.Driving license delhi.
Process C
opens the memory-mapped file and writes a value to it.Process A
reads and displays the values from the memory-mapped file.After
Process A
is finished with the memory-mapped file, the file is immediately reclaimed by garbage collection.
To run this example, do the following:
Compile the applications and open three Command Prompt windows.
In the first Command Prompt window, run
Process A
.In the second Command Prompt window, run
Process B
.Return to
Process A
and press ENTER.In the third Command Prompt window, run
Process C
.Return to
Process A
and press ENTER.
The output of Process A
is as follows:
Process A
Process B
Process C