A memory-mapped fle is a segment of virtual memory which has been
assigned a direct byte-for-byte correlation with some portion of a fle or fle-
like resource. This resource is typically a fle 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 fle descriptor. Once present, this correlation between the fle and the memory space permits applications to treat the mapped portion as if it were primary memory.The primary beneft of memory mapping a fle is increasing !O performance, especially when used on large fles. "or small fles, memory-mapped fles can result in a waste of slack space#$% as memory maps are always aligned to the page si&e, which is mostly ' (i). Therefore a * (i) fle will allocate + (i) and thus , (i) are wasted. Accessing memory mapped fles is faster than using direct read and write operations for two reasons. "irstly, a system call is orders of magnitude slower than a simple change to a program-s local memory. .econdly, in most operating systems the memory region mapped actually is the kernel-s page cache /fle cache0, meaning that no copies need to be created in user space. 1ertain application-level memory-mapped fle operations also perform better than their physical fle counterparts. Applications can access and update data in the fle directly and in-place, as opposed to seeking from the start of the fle or rewriting the entire edited contents to a temporary location. .ince the memory-mapped fle is handled internally in pages, linear fle access /as seen, for e2ample, in 3at fle data storage or confguration fles0 re4uires disk access only when a new page boundary is crossed, and can write larger sections of the fle to disk in a single operation. A possible beneft of memory-mapped fles is a 5la&y loading5, thus using small amounts of 6A7 even for a very large fle. Trying to load the entire contents of a fle that is signifcantly larger than the amount of memory available can cause severe thrashing as the operating system reads from disk into memory and simultaneously writes pages from memory back to disk. 7emory-mapping may not only bypass the page fle completely, but the system only needs to load the smaller page-si&ed sections as data is being edited, similarly to demand paging scheme used for programs. The memory mapping process is handled by the virtual memory manager, which is the same subsystem responsible for dealing with the page fle. 7emory mapped fles are loaded into memory one entire page at a time. The page si&e is selected by the operating system for ma2imum performance. .ince page fle management is one of the most critical elements of a virtual memory system, loading page si&ed sections of a fle into physical memory is typically a very highly optimi&ed system function.#8% 9rawbacks#edit% The major reason to choose memory mapped fle !O is performance. :evertheless, there can be tradeo;s. The standard !O approach is costly due to system call overhead and memory copying. The memory mapped approach has its cost in minor page faults - when a block of data is loaded in page cache, but is not yet mapped into the process-s virtual memory space. n some circumstances, memory mapped fle !O can be substantially slower than standard fle !O.#,% Another drawback of memory mapped fles relates to a given architecture-s address space< a fle larger than the addressable space can have only portions mapped at a time, complicating reading it. "or e2ample, a ,8-bit architecture such as ntel-s A-,8 can only directly address ' =i) or smaller portions of fles. An even smaller amount of addressible space is available to individual programs -- typically in the range of 8 to , =i), depending on the operating system kernel. 1ommon uses#edit% >erhaps the most common use for a memory-mapped fle is the process loader in most modern operating systems /including 7icrosoft ?indows and @ni2-like systems.0 ?hen a process is started, the operating system uses a memory mapped fle to bring the e2ecutable fle, along with any loadable modules, into memory for e2ecution. 7ost memory-mapping systems use a techni4ue called demand paging, where the fle is loaded into physical memory in subsets /one page each0, and only when that page is actually referenced.#'% n the specifc case of e2ecutable fles, this permits the O. to selectively load only those portions of a process image that actually need to e2ecute. Another common use for memory-mapped fles is to share memory between multiple processes. n 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.0 There are a number of techni4ues available to safely share memory, and memory-mapped fle !O is one of the most popular. Two or more applications can simultaneously map a single physical fle into memory and access this memory. "or e2ample, the 7icrosoft ?indows operating system provides a mechanism for applications to memory-map a shared segment of the system-s page fle itself and share data via this section. >latform support#edit% 7ost modern operating systems or runtime environments support some form of memory-mapped fle access. The function mmap/0,#*% which creates a mapping of a fle given a fle descriptor, starting location in the fle, and a length, is part of the >O.A specifcation, so the wide variety of >O.A- compliant systems, such as @:A, Binu2, 7ac O. A#C% or OpenD7., support a common mechanism for memory mapping fles. The 7icrosoft ?indows operating systems also support a group of A> functions for this purpose, such as 1reate"ile7apping/0.#E% The )oost 1FF Bibraries provide a portable implementation of memory- mapped fles for 7icrosoft ?indows and >O.A-compliant platforms.#+% Another minimalistic and portable implementation for 7icrosoft ?indows and >O.A-compliant platforms, based on the 1FF .tandard Template Bibrary /.TB0, is available on .ource"orge.#G% The Hava programming language provides classes and methods to access memory mapped fles, such as "ile1hannel. The 9 programming language supports memory mapped fles in its standard library /std.mmfle module0.#$I% 6uby has a gem /library0 called 7map, which implements memory-mapped fle objects. .ince version $.C, >ython has included a mmap module in its .tandard Bibrary.#$$% 9etails of the module vary according to whether the host platform is ?indows or @ni2-like. "or >erl there are a several modules available for memory mapping fles on the 1>A:, such as .ys<<7map#$8% and "ile<<7ap.#$,% n the 7icrosoft .:JT runtime, >!nvoke can be used to use memory mapped fles directly through the ?indows A>. 7anaged access />!nvoke not necessary0 to memory mapped fles was introduced in version ' of the runtime /see 7emory-7apped "iles0. "or previous versions, there are third- party libraries which provide managed A>-s.#$'% >K> supported memory-mapping techni4ues in a number of native fle access functions such as fleLgetLcontents/0 but has removed this in *., /see revision log0. "or the 6 programming language there e2ists a library on 16A: called bigmemory which uses the )oost library and provides memory-mapped backed arrays directly in 6.