Fetching contributors…
Cannot retrieve contributors at this time
144 lines (92 sloc) 10.7 KB
title dev_langs helpviewer_keywords ms.assetid author
Memory-Mapped Files
memory-mapped files
inter-process communication

Memory-Mapped Files

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 [!INCLUDEnet_v40_long], 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.

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.

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.

Shows views to a memory-mapped file.
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 xref:System.IO.MemoryMappedFiles.MemoryMappedFile object that represents a persisted memory-mapped file from a file on disk. xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile%2A?displayProperty=nameWithType method.
To obtain a xref:System.IO.MemoryMappedFiles.MemoryMappedFile object that represents a non-persisted memory-mapped file (not associated with a file on disk). xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew%2A?displayProperty=nameWithType method.

- or -

xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen%2A?displayProperty=nameWithType method.
To obtain a xref:System.IO.MemoryMappedFiles.MemoryMappedFile object of an existing memory-mapped file (either persisted or non-persisted). xref:System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting%2A?displayProperty=nameWithType method.
To obtain a xref:System.IO.UnmanagedMemoryStream object for a sequentially accessed view to the memory-mapped file. xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewStream%2A?displayProperty=nameWithType method.
To obtain a xref:System.IO.UnmanagedMemoryAccessor object for a random access view to a memory-mapped fie. xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewAccessor%2A?displayProperty=nameWithType method.
To obtain a xref:Microsoft.Win32.SafeHandles.SafeMemoryMappedViewHandle object to use with unmanaged code. xref:System.IO.MemoryMappedFiles.MemoryMappedFile.SafeMemoryMappedFileHandle%2A?displayProperty=nameWithType property.

- or -

xref:System.IO.MemoryMappedFiles.MemoryMappedViewAccessor.SafeMemoryMappedViewHandle%2A?displayProperty=nameWithType property.

- or -

xref:System.IO.MemoryMappedFiles.MemoryMappedViewStream.SafeMemoryMappedViewHandle%2A?displayProperty=nameWithType property.
To delay allocating memory until a view is created (non-persisted files only).

(To determine the current system page size, use the xref:System.Environment.SystemPageSize%2A?displayProperty=nameWithType property.)
xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew%2A method with the xref:System.IO.MemoryMappedFiles.MemoryMappedFileOptions.DelayAllocatePages?displayProperty=nameWithType value.

- or -

xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen%2A methods that have a xref:System.IO.MemoryMappedFiles.MemoryMappedFileOptions enumeration as a parameter.


You can apply access rights when you create a memory-mapped file, by using the following methods that take a xref:System.IO.MemoryMappedFiles.MemoryMappedFileAccess enumeration as a parameter:

  • xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile%2A?displayProperty=nameWithType

  • xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew%2A?displayProperty=nameWithType

  • xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen%2A?displayProperty=nameWithType

You can specify access rights for opening an existing memory-mapped file by using the xref:System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting%2A methods that take an xref:System.IO.MemoryMappedFiles.MemoryMappedFileRights as a parameter.

In addition, you can include a xref:System.IO.MemoryMappedFiles.MemoryMappedFileSecurity object that contains predefined access rules.

To apply new or changed access rules to a memory-mapped file, use the xref:System.IO.MemoryMappedFiles.MemoryMappedFile.SetAccessControl%2A method. To retrieve access or audit rules from an existing file, use the xref:System.IO.MemoryMappedFiles.MemoryMappedFile.GetAccessControl%2A method.


Persisted Memory-Mapped Files

The xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile%2A 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.

[!code-csharpMemoryMappedFiles.MemoryMappedFile.CreateFromFile#1] [!code-vbMemoryMappedFiles.MemoryMappedFile.CreateFromFile#1]

The following example opens the same memory-mapped file for another process.

[!code-csharpMemoryMappedFiles.MemoryMappedFile.OpenExisting#1] [!code-vbMemoryMappedFiles.MemoryMappedFile.OpenExisting#1]

Non-Persisted Memory-Mapped Files

The xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew%2A and xref:System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen%2A 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:

  1. Process A creates the memory-mapped file and writes a value to it.

  2. Process B opens the memory-mapped file and writes a value to it.

  3. Process C opens the memory-mapped file and writes a value to it.

  4. Process A reads and displays the values from the memory-mapped file.

  5. 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:

  1. Compile the applications and open three Command Prompt windows.

  2. In the first Command Prompt window, run Process A.

  3. In the second Command Prompt window, run Process B.

  4. Return to Process A and press ENTER.

  5. In the third Command Prompt window, run Process C.

  6. Return to Process A and press ENTER.

The output of Process A is as follows:

Start Process B and press ENTER to continue.  
Start Process C and press ENTER to continue.  
Process A says: True  
Process B says: False  
Process C says: True  

Process A

[!code-csharpSystem.IO.MemoryMappedFiles_IPC_X#1] [!code-vbSystem.IO.MemoryMappedFiles_IPC_X#1]

Process B

[!code-csharpSystem.IO.MemoryMappedFiles_IPC_A#1] [!code-vbSystem.IO.MemoryMappedFiles_IPC_A#1]

Process C

[!code-csharpSystem.IO.MemoryMappedFiles_IPC_B#1] [!code-vbSystem.IO.MemoryMappedFiles_IPC_B#1]

See also