Jump to content

Memory-mapped file: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
rv linkspam Undid revision 113336218 by 159.53.78.141 (talk)
Line 15: Line 15:
==Platform support==
==Platform support==


Most modern operating systems or runtime environments support some form of memory-mapped file access. The [[C (programming language)|ISO C Standard]] includes an optional extension to the standard library that includes the function <tt>[[mmap]]</tt>, which creates memory-mapped files from any valid file descriptor <ref>http://www.ecst.csuchico.edu/~beej/guide/ipc/mmap.html</ref>. This extension is also part of the [[POSIX]] specification, so the wide variety of POSIX-compliant systems, such as UNIX, [[Linux]], or [[OpenVMS]], support a common mechanism for memory mapping files. The Microsoft Windows operating systems also support a group of [[API]] functions for this purpose, such as <tt>CreateFileMapping()</tt> <ref>http://msdn2.microsoft.com/en-us/library/aa366537.aspx</ref>. The Microsoft .NET runtime environment does not natively include managed access to memory mapped files, but there are third-party libraries which do so <ref>http://www.winterdom.com/dev/dotnet/index.html</ref>, <ref>http://www.morantex.com</ref>
Most modern operating systems or runtime environments support some form of memory-mapped file access. The [[C (programming language)|ISO C Standard]] includes an optional extension to the standard library that includes the function <tt>[[mmap]]</tt>, which creates memory-mapped files from any valid file descriptor <ref>http://www.ecst.csuchico.edu/~beej/guide/ipc/mmap.html</ref>. This extension is also part of the [[POSIX]] specification, so the wide variety of POSIX-compliant systems, such as UNIX, [[Linux]], or [[OpenVMS]], support a common mechanism for memory mapping files. The Microsoft Windows operating systems also support a group of [[API]] functions for this purpose, such as <tt>CreateFileMapping()</tt> <ref>http://msdn2.microsoft.com/en-us/library/aa366537.aspx</ref>. The Microsoft .NET runtime environment does not natively include managed access to memory mapped files, but there are third-party libraries which do so <ref>http://www.winterdom.com/dev/dotnet/index.html</ref>


==References==
==References==

Revision as of 19:02, 7 March 2007

A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource is typically a file that is physically present on-disk, but can also be a device, shared memory object, or other resource that the operating system can reference through a file descriptor. Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.

Benefits

The primary benefit of memory mapping a file is increased performance, especially when used on small files. The memory mapping process is handled by the virtual memory manager, which is the same subsystem responsible for dealing with the page file. Memory mapped files are loaded into memory one entire page at a time. The page size is selected by the operating system for maximum performance, and I/O of smaller amounts of data are typically cached and done several at a time. Since page file management is one of the most critical elements of a virtual memory system, loading page sized sections of a file into physical memory is typically a very highly optimized system function[1]

Certain application level memory-mapped file operations also perform better than their physical file counterparts. Most notably, memory-mapped files allow for high performance random-access writing to a disk file. The application can access and update data in the file directly and in-place, as opposed to seeking from the start of the file or rewriting the entire edited contents to a temporary location. Since the memory-mapped file is handled internally in pages, linear file access (as seen, for example, in flat file data storage or configuration files) requires disk access only when a new page boundary is crossed, and can write larger sections of the file to disk in a single operation.

A final benefit of memory-mapped file I/O is the automatic management of large files in smaller chunks. When dealing with files that are a significant fraction of the system's total memory size, trying to load the entire on-disk contents into memory can cause severe thrashing as the operating system reads from disk into memory and simultaneously pages from memory back to disk. By memory-mapping the large file, not only is the page file bypassed completely, but the system only needs to load the smaller page-sized sections being read or written at any given time.

Common uses

Perhaps the most common use for a memory-mapped file is the process loader in most modern operating systems (including Microsoft Windows and UNIX-like systems.) When a process is started, the operating system uses a memory mapped file to bring the executable file, along with any loadable modules, into memory for execution. Most memory-mapping systems use a technique called demand paging, where the file is loaded into physical memory in subsets (one page each), and only when that page is actually referenced. [2]. In the specific case of executable files, this permits the OS to selectively load only those portions of a process image that actually need to execute.

Another common use for memory-mapped files is to share memory between multiple processes. In modern protected mode operating systems, processes are generally not permitted to access memory space that is allocated for use by another process. (A program's attempt to do so causes invalid page faults or segmentation violations.) There are a number of techniques available to safely share memory, and memory-mapped file I/O is one of the most popular. Two or more applications can simultaneously map a single physical file into memory and access this memory. For example, the Microsoft Windows operating system provides a mechanism for applications to memory-map a shared segment of the system's page file itself and share data via this section.

Platform support

Most modern operating systems or runtime environments support some form of memory-mapped file access. The ISO C Standard includes an optional extension to the standard library that includes the function mmap, which creates memory-mapped files from any valid file descriptor [3]. This extension is also part of the POSIX specification, so the wide variety of POSIX-compliant systems, such as UNIX, Linux, or OpenVMS, support a common mechanism for memory mapping files. The Microsoft Windows operating systems also support a group of API functions for this purpose, such as CreateFileMapping() [4]. The Microsoft .NET runtime environment does not natively include managed access to memory mapped files, but there are third-party libraries which do so [5]

References