Memory-mapped File Website Links For
File
 

Information About

Memory-mapped File





BENEFITS

The primary benefit of memory mapping a file is increased I/O performance, especially when used on small files. Accessing memory mapped files is faster than accessing them on disk for two reasons. First, disk access requires a system call which is slower than a local function call. Changes to memory mapped file data are typically cached and update to disk in batches. This reduces the frequency of time consuming disk I/O operations. Second, most operating systems provide shared access to the file buffer (the kernel's cache of file data), meaning that no copies need to be created in user space. Memory copying is another time consuming operation.

Certain application level memory-mapped file operations also perform better than their physical file counterparts. Applications 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 files is the automatic management of large files in smaller chunks. Trying to load the entire contents of a file that is significantly larger than the amount of memory available can cause severe Thrashing as the operating system reads from disk into memory and simultaneously pages from memory back to disk. Not only does memory-mapping bypass the page file completely, the system only needs to load the smaller page-sized sections where data is being edited.

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. 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 functionhttp://msdn2.microsoft.com/en-us/library/ms810613.aspx, "What Do Memory-Mapped Files Have to Offer?".


DRAWBACKS

The major reason to choose memory mapped file I/O is for performance. One should nevertheless keep in mind the tradeoff that is being made. The standard I/O approach is costly due to system call overhead and memory copying. The memory mapped approach has its cost in page faults - when a piece of data isn't actually loaded by the operating system - and this cost can be high. Depending on the circumstances, memory mapped file I/O can actually be substantially slower than standard file I/O. For example: when reading in very large files, most of the data will not be cached by the kernel, meaning many page faults will occur when reading uncached data.


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. http://www.linux-tutorial.info/modules.php?name=Tutorial&pageid=89, "Demand Paging". 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 Violation s.) 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 function 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() http://msdn2.microsoft.com/en-us/library/aa366537.aspx. The Microsoft .NET runtime environment does not natively include managed access to memory mapped files, but there are third-party libraries which do so http://www.winterdom.com/dev/dotnet/index.html


REFERENCES