-->
Memory-Mapped Files. Memory-mapped files are not new. For over 20 years, the Windows operating system allowed applications to map virtual addresses directly to a file on disk thereby allowing multiple processes to share it. File-based data looked and, more importantly, performed like system virtual memory. There was another benefit. Creating Named Shared Memory.; 2 minutes to read; In this article. To share data, multiple processes can use memory-mapped files that the system paging file stores. First Process. The first process creates the file mapping object by calling the CreateFileMapping function with INVALIDHANDLEVALUE and a name for the object.
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.
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.
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:
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.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
Linux C Memory Mapped File
See also
-->Definition
C Read Memory Mapped File
Mapped File Memory Windows 10
Examples
The following example creates a memory-mapped view of a part of an extremely large file and manipulates a portion of it.
Remarks
Mmap Memory Mapped File
A memory-mapped file maps the contents of a file to an application's logical address space. Memory-mapped files enable programmers to work with extremely large files because memory can be managed concurrently, and they allow complete, random access to a file without the need for seeking. Memory-mapped files can also be shared across multiple processes.
The CreateFromFile methods create a memory-mapped file from a specified path or a FileStream of an existing file on disk. Changes are automatically propagated to disk when the file is unmapped.
The CreateNew methods create a memory-mapped file that is not mapped to an existing file on disk; and are suitable for creating shared memory for interprocess communication (IPC).
A memory-mapped file can be associated with an optional name that allows the memory-mapped file to be shared with other processes.
You can create multiple views of the memory-mapped file, including views of parts of the file. You can map the same part of a file to more than one address to create concurrent memory. For two views to remain concurrent, they have to be created from the same memory-mapped file. Creating two file mappings of the same file with two views does not provide concurrency.
Properties
Memory Mapped File C Library
SafeMemoryMappedFileHandleSafeMemoryMappedFileHandleSafeMemoryMappedFileHandleSafeMemoryMappedFileHandle | Gets the file handle of a memory-mapped file. |
C Memory Mapped File Example
Methods
CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean)CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, HandleInheritability, Boolean) | Creates a memory-mapped file from an existing file with the specified access mode, name, inheritability, and capacity. |
CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean)CreateFromFile(FileStream, String, Int64, MemoryMappedFileAccess, MemoryMappedFileSecurity, HandleInheritability, Boolean) | Creates a memory-mapped file that has the specified name, capacity, access type, security permissions, inheritability, and disposal requirement from a file on disk. |
CreateFromFile(String)CreateFromFile(String)CreateFromFile(String)CreateFromFile(String) | Creates a memory-mapped file from a file on disk. |
CreateFromFile(String, FileMode)CreateFromFile(String, FileMode)CreateFromFile(String, FileMode)CreateFromFile(String, FileMode) | Creates a memory-mapped file that has the specified access mode from a file on disk. |
CreateFromFile(String, FileMode, String)CreateFromFile(String, FileMode, String)CreateFromFile(String, FileMode, String)CreateFromFile(String, FileMode, String) | Creates a memory-mapped file that has the specified access mode and name from a file on disk. |
CreateFromFile(String, FileMode, String, Int64)CreateFromFile(String, FileMode, String, Int64)CreateFromFile(String, FileMode, String, Int64)CreateFromFile(String, FileMode, String, Int64) | Creates a memory-mapped file that has the specified access mode, name, and capacity from a file on disk. |
CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess)CreateFromFile(String, FileMode, String, Int64, MemoryMappedFileAccess) | Creates a memory-mapped file that has the specified access mode, name, capacity, and access type from a file on disk. |
CreateNew(String, Int64)CreateNew(String, Int64)CreateNew(String, Int64)CreateNew(String, Int64) | Creates a memory-mapped file that has the specified capacity in system memory. |
CreateNew(String, Int64, MemoryMappedFileAccess)CreateNew(String, Int64, MemoryMappedFileAccess)CreateNew(String, Int64, MemoryMappedFileAccess)CreateNew(String, Int64, MemoryMappedFileAccess) | Creates a memory-mapped file that has the specified capacity and access type in system memory. |
CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability) | Creates a memory-mapped file that has the specified name, capacity, access type, memory allocation options and inheritability. |
CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)CreateNew(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability) | Creates a memory-mapped file that has the specified capacity, access type, memory allocation, security permissions, and inheritability in system memory. |
CreateOrOpen(String, Int64)CreateOrOpen(String, Int64)CreateOrOpen(String, Int64)CreateOrOpen(String, Int64) | Creates or opens a memory-mapped file that has the specified name and capacity in system memory. |
CreateOrOpen(String, Int64, MemoryMappedFileAccess)CreateOrOpen(String, Int64, MemoryMappedFileAccess)CreateOrOpen(String, Int64, MemoryMappedFileAccess)CreateOrOpen(String, Int64, MemoryMappedFileAccess) | Creates or opens a memory-mapped file that has the specified name, capacity and access type in system memory. |
CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability)CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, HandleInheritability) | Creates a new empty memory mapped file or opens an existing memory mapped file if one exists with the same name. If opening an existing file, the capacity, options, and memory arguments will be ignored. |
CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability)CreateOrOpen(String, Int64, MemoryMappedFileAccess, MemoryMappedFileOptions, MemoryMappedFileSecurity, HandleInheritability) | Creates or opens a memory-mapped file that has the specified name, capacity, access type, memory allocation, security permissions, and inheritability in system memory. |
CreateViewAccessor()CreateViewAccessor()CreateViewAccessor()CreateViewAccessor() | Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file. |
CreateViewAccessor(Int64, Int64)CreateViewAccessor(Int64, Int64)CreateViewAccessor(Int64, Int64)CreateViewAccessor(Int64, Int64) | Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset and size. |
CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess)CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess)CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess)CreateViewAccessor(Int64, Int64, MemoryMappedFileAccess) | Creates a MemoryMappedViewAccessor that maps to a view of the memory-mapped file, and that has the specified offset, size, and access restrictions. |
CreateViewStream()CreateViewStream()CreateViewStream()CreateViewStream() | Creates a stream that maps to a view of the memory-mapped file. |
CreateViewStream(Int64, Int64)CreateViewStream(Int64, Int64)CreateViewStream(Int64, Int64)CreateViewStream(Int64, Int64) | Creates a stream that maps to a view of the memory-mapped file, and that has the specified offset and size. |
CreateViewStream(Int64, Int64, MemoryMappedFileAccess)CreateViewStream(Int64, Int64, MemoryMappedFileAccess)CreateViewStream(Int64, Int64, MemoryMappedFileAccess)CreateViewStream(Int64, Int64, MemoryMappedFileAccess) | Creates a stream that maps to a view of the memory-mapped file, and that has the specified offset, size, and access type. |
Dispose()Dispose()Dispose()Dispose() | Releases all resources used by the MemoryMappedFile. |
Dispose(Boolean)Dispose(Boolean)Dispose(Boolean)Dispose(Boolean) | Releases the unmanaged resources used by the MemoryMappedFile and optionally releases the managed resources. |
Equals(Object)Equals(Object)Equals(Object)Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetAccessControl()GetAccessControl()GetAccessControl()GetAccessControl() | Gets the access control to the memory-mapped file resource. |
GetHashCode()GetHashCode()GetHashCode()GetHashCode() | Serves as the default hash function. (Inherited from Object) |
GetType()GetType()GetType()GetType() | Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone()MemberwiseClone()MemberwiseClone()MemberwiseClone() | Creates a shallow copy of the current Object. (Inherited from Object) |
OpenExisting(String)OpenExisting(String)OpenExisting(String)OpenExisting(String) | Opens an existing memory-mapped file that has the specified name in system memory. |
OpenExisting(String, MemoryMappedFileRights)OpenExisting(String, MemoryMappedFileRights)OpenExisting(String, MemoryMappedFileRights)OpenExisting(String, MemoryMappedFileRights) | Opens an existing memory-mapped file that has the specified name and access rights in system memory. |
OpenExisting(String, MemoryMappedFileRights, HandleInheritability)OpenExisting(String, MemoryMappedFileRights, HandleInheritability)OpenExisting(String, MemoryMappedFileRights, HandleInheritability)OpenExisting(String, MemoryMappedFileRights, HandleInheritability) | Opens an existing memory-mapped file that has the specified name, access rights, and inheritability in system memory. |
SetAccessControl(MemoryMappedFileSecurity)SetAccessControl(MemoryMappedFileSecurity)SetAccessControl(MemoryMappedFileSecurity)SetAccessControl(MemoryMappedFileSecurity) | Sets the access control to the memory-mapped file resource. |
ToString()ToString()ToString()ToString() | Returns a string that represents the current object. (Inherited from Object) |