<s>
A	O
memory-mapped	B-General_Concept
file	I-General_Concept
is	O
a	O
segment	O
of	O
virtual	B-Architecture
memory	I-Architecture
that	O
has	O
been	O
assigned	O
a	O
direct	O
byte-for-byte	O
correlation	O
with	O
some	O
portion	O
of	O
a	O
file	O
or	O
file-like	O
resource	O
.	O
</s>
<s>
This	O
resource	O
is	O
typically	O
a	O
file	O
that	O
is	O
physically	O
present	O
on	O
disk	O
,	O
but	O
can	O
also	O
be	O
a	O
device	O
,	O
shared	B-Operating_System
memory	I-Operating_System
object	O
,	O
or	O
other	O
resource	O
that	O
the	O
operating	B-General_Concept
system	I-General_Concept
can	O
reference	O
through	O
a	O
file	B-Application
descriptor	I-Application
.	O
</s>
<s>
An	O
early	O
(	O
)	O
implementation	O
of	O
this	O
was	O
the	O
PMAP	O
system	O
call	O
on	O
the	O
DEC-20	B-Device
'	O
s	O
TOPS-20	B-Operating_System
operating	B-General_Concept
system	I-General_Concept
,	O
a	O
feature	O
used	O
by	O
Software	O
House	O
's	O
System-1022	O
database	O
system	O
.	O
</s>
<s>
SunOS	O
4	O
introduced	O
Unix	B-Application
's	O
mmap	B-Operating_System
,	O
which	O
permitted	O
programs	O
"	O
to	O
map	O
files	O
into	O
memory.	O
"	O
</s>
<s>
Two	O
decades	O
after	O
the	O
release	O
of	O
TOPS-20	B-Operating_System
'	O
s	O
PMAP	O
,	O
Windows	B-Device
NT	I-Device
was	O
given	O
Growable	O
Memory-Mapped	B-General_Concept
Files	I-General_Concept
(	O
GMMF	O
)	O
.	O
</s>
<s>
a	O
file	O
's	O
size	O
is	O
not	O
readily	O
accommodated	O
,	O
a	O
GMMF	O
API	B-General_Concept
was	O
developed	O
.	O
</s>
<s>
For	O
small	O
files	O
,	O
memory-mapped	B-General_Concept
files	I-General_Concept
can	O
result	O
in	O
a	O
waste	O
of	O
slack	O
space	O
as	O
memory	B-Architecture
maps	I-Architecture
are	O
always	O
aligned	O
to	O
the	O
page	B-General_Concept
size	I-General_Concept
,	O
which	O
is	O
mostly	O
4	O
KiB	O
.	O
</s>
<s>
Accessing	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
is	O
faster	O
than	O
using	O
direct	O
read	O
and	O
write	O
operations	O
for	O
two	O
reasons	O
.	O
</s>
<s>
Secondly	O
,	O
in	O
most	O
operating	B-General_Concept
systems	I-General_Concept
the	O
memory	O
region	O
mapped	O
actually	O
is	O
the	O
kernel	O
's	O
page	B-General_Concept
cache	I-General_Concept
(	O
file	O
cache	O
)	O
,	O
meaning	O
that	O
no	O
copies	O
need	O
to	O
be	O
created	O
in	O
user	O
space	O
.	O
</s>
<s>
Certain	O
application-level	O
memory-mapped	B-General_Concept
file	I-General_Concept
operations	O
also	O
perform	O
better	O
than	O
their	O
physical	O
file	O
counterparts	O
.	O
</s>
<s>
Since	O
the	O
memory-mapped	B-General_Concept
file	I-General_Concept
is	O
handled	O
internally	O
in	O
pages	O
,	O
linear	O
file	O
access	O
(	O
as	O
seen	O
,	O
for	O
example	O
,	O
in	O
flat	B-Application
file	I-Application
data	O
storage	O
or	O
configuration	O
files	O
)	O
requires	O
disk	O
access	O
only	O
when	O
a	O
new	O
page	B-General_Concept
boundary	O
is	O
crossed	O
,	O
and	O
can	O
write	O
larger	O
sections	O
of	O
the	O
file	O
to	O
disk	O
in	O
a	O
single	O
operation	O
.	O
</s>
<s>
A	O
possible	O
benefit	O
of	O
memory-mapped	B-General_Concept
files	I-General_Concept
is	O
a	O
"	O
lazy	O
loading	O
"	O
,	O
thus	O
using	O
small	O
amounts	O
of	O
RAM	O
even	O
for	O
a	O
very	O
large	O
file	O
.	O
</s>
<s>
Trying	O
to	O
load	O
the	O
entire	O
contents	O
of	O
a	O
file	O
that	O
is	O
significantly	O
larger	O
than	O
the	O
amount	O
of	O
memory	O
available	O
can	O
cause	O
severe	O
thrashing	B-General_Concept
as	O
the	O
operating	B-General_Concept
system	I-General_Concept
reads	O
from	O
disk	O
into	O
memory	O
and	O
simultaneously	O
writes	O
pages	O
from	O
memory	O
back	O
to	O
disk	O
.	O
</s>
<s>
Memory-mapping	O
may	O
not	O
only	O
bypass	O
the	O
page	B-Architecture
file	I-Architecture
completely	O
,	O
but	O
also	O
allow	O
smaller	O
page-sized	O
sections	O
to	O
be	O
loaded	O
as	O
data	O
is	O
being	O
edited	O
,	O
similarly	O
to	O
demand	B-General_Concept
paging	I-General_Concept
used	O
for	O
programs	O
.	O
</s>
<s>
The	O
memory	O
mapping	O
process	B-Operating_System
is	O
handled	O
by	O
the	O
virtual	B-Architecture
memory	I-Architecture
manager	I-Architecture
,	O
which	O
is	O
the	O
same	O
subsystem	O
responsible	O
for	O
dealing	O
with	O
the	O
page	B-Architecture
file	I-Architecture
.	O
</s>
<s>
Memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
are	O
loaded	O
into	O
memory	O
one	O
entire	O
page	B-General_Concept
at	O
a	O
time	O
.	O
</s>
<s>
The	O
page	B-General_Concept
size	I-General_Concept
is	O
selected	O
by	O
the	O
operating	B-General_Concept
system	I-General_Concept
for	O
maximum	O
performance	O
.	O
</s>
<s>
Since	O
page	B-Architecture
file	I-Architecture
management	O
is	O
one	O
of	O
the	O
most	O
critical	O
elements	O
of	O
a	O
virtual	B-Operating_System
memory	I-Operating_System
system	I-Operating_System
,	O
loading	O
page	B-General_Concept
sized	O
sections	O
of	O
a	O
file	O
into	O
physical	O
memory	O
is	O
typically	O
a	O
very	O
highly	O
optimized	O
system	O
function	O
.	O
</s>
<s>
There	O
are	O
two	O
types	O
of	O
memory-mapped	B-General_Concept
files	I-General_Concept
:	O
</s>
<s>
The	O
data	O
is	O
saved	O
to	O
the	O
source	O
file	O
on	O
the	O
disk	O
once	O
the	O
last	O
process	B-Operating_System
is	O
finished	O
.	O
</s>
<s>
These	O
memory-mapped	B-General_Concept
files	I-General_Concept
are	O
suitable	O
for	O
working	O
with	O
extremely	O
large	O
source	O
files	O
.	O
</s>
<s>
When	O
the	O
last	O
process	B-Operating_System
has	O
finished	O
working	O
with	O
the	O
file	O
,	O
the	O
data	O
is	O
lost	O
.	O
</s>
<s>
These	O
files	O
are	O
suitable	O
for	O
creating	O
shared	B-Operating_System
memory	I-Operating_System
for	O
inter-process	O
communications	O
(	O
IPC	O
)	O
.	O
</s>
<s>
The	O
major	O
reason	O
to	O
choose	O
memory	B-General_Concept
mapped	I-General_Concept
file	I-General_Concept
I/O	O
is	O
performance	O
.	O
</s>
<s>
The	O
memory-mapped	O
approach	O
has	O
its	O
cost	O
in	O
minor	O
page	B-General_Concept
faults	O
—	O
when	O
a	O
block	O
of	O
data	O
is	O
loaded	O
in	O
page	B-General_Concept
cache	I-General_Concept
,	O
but	O
is	O
not	O
yet	O
mapped	O
into	O
the	O
process	B-Operating_System
's	O
virtual	B-Architecture
memory	I-Architecture
space	O
.	O
</s>
<s>
In	O
some	O
circumstances	O
,	O
memory	B-General_Concept
mapped	I-General_Concept
file	I-General_Concept
I/O	O
can	O
be	O
substantially	O
slower	O
than	O
standard	O
file	O
I/O	O
.	O
</s>
<s>
Another	O
drawback	O
of	O
memory-mapped	B-General_Concept
files	I-General_Concept
relates	O
to	O
a	O
given	O
architecture	O
's	O
address	B-General_Concept
space	I-General_Concept
:	O
a	O
file	O
larger	O
than	O
the	O
addressable	O
space	O
can	O
have	O
only	O
portions	O
mapped	O
at	O
a	O
time	O
,	O
complicating	O
reading	O
it	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
32-bit	O
architecture	O
such	O
as	O
Intel	O
's	O
IA-32	B-Device
can	O
only	O
directly	O
address	O
4	O
GiB	O
or	O
smaller	O
portions	O
of	O
files	O
.	O
</s>
<s>
An	O
even	O
smaller	O
amount	O
of	O
addressable	O
space	O
is	O
available	O
to	O
individual	O
programs	O
—	O
typically	O
in	O
the	O
range	O
of	O
2	O
to	O
3	O
GiB	O
,	O
depending	O
on	O
the	O
operating	B-General_Concept
system	I-General_Concept
kernel	O
.	O
</s>
<s>
This	O
drawback	O
however	O
is	O
virtually	O
eliminated	O
on	O
modern	O
64-bit	B-Device
architecture	I-Device
.	O
</s>
<s>
mmap	B-Operating_System
also	O
tends	O
to	O
be	O
less	O
scalable	O
than	O
standard	O
means	O
of	O
file	O
I/O	O
,	O
since	O
many	O
operating	B-General_Concept
systems	I-General_Concept
,	O
including	O
Linux	B-Application
,	O
has	O
a	O
cap	O
on	O
the	O
number	O
of	O
cores	O
handling	O
page	B-General_Concept
faults	O
.	O
</s>
<s>
Extremely	O
fast	O
devices	O
,	O
such	O
as	O
modern	O
NVM	B-Application
Express	I-Application
SSDs	O
,	O
are	O
capable	O
of	O
making	O
the	O
overhead	O
a	O
real	O
concern	O
.	O
</s>
<s>
while	O
accessing	O
its	O
mapped	O
memory	O
are	O
reported	O
to	O
the	O
application	O
as	O
the	O
SIGSEGV/SIGBUS	O
signals	O
on	O
POSIX	O
,	O
and	O
the	O
EXECUTE_IN_PAGE_ERROR	O
structured	O
exception	O
on	O
Windows	B-Application
.	O
</s>
<s>
Only	O
hardware	O
architectures	O
with	O
an	O
MMU	B-General_Concept
can	O
support	O
memory-mapped	B-General_Concept
files	I-General_Concept
.	O
</s>
<s>
On	O
architectures	O
without	O
an	O
MMU	B-General_Concept
,	O
the	O
operating	B-General_Concept
system	I-General_Concept
can	O
copy	O
the	O
entire	O
file	O
into	O
memory	O
when	O
the	O
request	O
to	O
map	O
it	O
is	O
made	O
,	O
but	O
this	O
is	O
extremely	O
wasteful	O
and	O
slow	O
if	O
only	O
a	O
little	O
bit	O
of	O
the	O
file	O
will	O
be	O
accessed	O
,	O
and	O
can	O
only	O
work	O
for	O
files	O
that	O
will	O
fit	O
in	O
available	O
memory	O
.	O
</s>
<s>
Perhaps	O
the	O
most	O
common	O
use	O
for	O
a	O
memory-mapped	B-General_Concept
file	I-General_Concept
is	O
the	O
process	B-Operating_System
loader	I-Operating_System
in	O
most	O
modern	O
operating	B-General_Concept
systems	I-General_Concept
(	O
including	O
Microsoft	B-Application
Windows	I-Application
and	O
Unix-like	B-Operating_System
systems	I-Operating_System
.	O
)	O
</s>
<s>
When	O
a	O
process	B-Operating_System
is	O
started	O
,	O
the	O
operating	B-General_Concept
system	I-General_Concept
uses	O
a	O
memory	B-General_Concept
mapped	I-General_Concept
file	I-General_Concept
to	O
bring	O
the	O
executable	O
file	O
,	O
along	O
with	O
any	O
loadable	O
modules	O
,	O
into	O
memory	O
for	O
execution	O
.	O
</s>
<s>
Most	O
memory-mapping	O
systems	O
use	O
a	O
technique	O
called	O
demand	B-General_Concept
paging	I-General_Concept
,	O
where	O
the	O
file	O
is	O
loaded	O
into	O
physical	O
memory	O
in	O
subsets	O
(	O
one	O
page	B-General_Concept
each	O
)	O
,	O
and	O
only	O
when	O
that	O
page	B-General_Concept
is	O
actually	O
referenced	O
.	O
</s>
<s>
In	O
the	O
specific	O
case	O
of	O
executable	O
files	O
,	O
this	O
permits	O
the	O
OS	O
to	O
selectively	O
load	O
only	O
those	O
portions	O
of	O
a	O
process	B-Operating_System
image	O
that	O
actually	O
need	O
to	O
execute	O
.	O
</s>
<s>
Another	O
common	O
use	O
for	O
memory-mapped	B-General_Concept
files	I-General_Concept
is	O
to	O
share	O
memory	O
between	O
multiple	O
processes	O
.	O
</s>
<s>
In	O
modern	O
protected	B-Application
mode	I-Application
operating	B-General_Concept
systems	I-General_Concept
,	O
processes	O
are	O
generally	O
not	O
permitted	O
to	O
access	O
memory	O
space	O
that	O
is	O
allocated	O
for	O
use	O
by	O
another	O
process	B-Operating_System
.	O
</s>
<s>
(	O
A	O
program	O
's	O
attempt	O
to	O
do	O
so	O
causes	O
invalid	O
page	B-General_Concept
faults	O
or	O
segmentation	B-Error_Name
violations	I-Error_Name
.	O
)	O
</s>
<s>
There	O
are	O
a	O
number	O
of	O
techniques	O
available	O
to	O
safely	O
share	O
memory	O
,	O
and	O
memory-mapped	B-General_Concept
file	I-General_Concept
I/O	O
is	O
one	O
of	O
the	O
most	O
popular	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
Microsoft	B-Application
Windows	I-Application
operating	I-Application
system	I-Application
provides	O
a	O
mechanism	O
for	O
applications	O
to	O
memory-map	O
a	O
shared	O
segment	O
of	O
the	O
system	O
's	O
page	B-Architecture
file	I-Architecture
itself	O
and	O
share	O
data	O
via	O
this	O
section	O
.	O
</s>
<s>
Most	O
modern	O
operating	B-General_Concept
systems	I-General_Concept
or	O
runtime	O
environments	O
support	O
some	O
form	O
of	O
memory-mapped	B-General_Concept
file	I-General_Concept
access	O
.	O
</s>
<s>
The	O
function	O
,	O
which	O
creates	O
a	O
mapping	O
of	O
a	O
file	O
given	O
a	O
file	B-Application
descriptor	I-Application
,	O
starting	O
location	O
in	O
the	O
file	O
,	O
and	O
a	O
length	O
,	O
is	O
part	O
of	O
the	O
POSIX	O
specification	O
,	O
so	O
the	O
wide	O
variety	O
of	O
POSIX-compliant	O
systems	O
,	O
such	O
as	O
UNIX	B-Application
,	O
Linux	B-Application
,	O
Mac	B-Operating_System
OS	I-Operating_System
X	I-Operating_System
or	O
OpenVMS	B-Operating_System
,	O
support	O
a	O
common	O
mechanism	O
for	O
memory	O
mapping	O
files	O
.	O
</s>
<s>
The	O
Microsoft	B-Application
Windows	I-Application
operating	I-Application
systems	I-Application
also	O
support	O
a	O
group	O
of	O
API	B-General_Concept
functions	O
for	O
this	O
purpose	O
,	O
such	O
as	O
.	O
</s>
<s>
Some	O
free	O
portable	O
implementations	O
of	O
memory-mapped	B-General_Concept
files	I-General_Concept
for	O
Microsoft	B-Application
Windows	I-Application
and	O
POSIX-compliant	O
platforms	O
are	O
:	O
</s>
<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
provides	O
classes	O
and	O
methods	O
to	O
access	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
,	O
such	O
as	O
.	O
</s>
<s>
The	O
D	B-Application
programming	I-Application
language	I-Application
supports	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
in	O
its	O
standard	O
library	O
(	O
std.mmfile	O
module	O
)	O
.	O
</s>
<s>
Ruby	B-Language
has	O
a	O
gem	O
(	O
library	O
)	O
called	O
Mmap	B-Operating_System
,	O
which	O
implements	O
memory-mapped	B-General_Concept
file	I-General_Concept
objects	O
.	O
</s>
<s>
Since	O
version	O
1.6	O
,	O
Python	B-Language
has	O
included	O
a	O
module	O
in	O
its	O
Standard	O
Library	O
.	O
</s>
<s>
Details	O
of	O
the	O
module	O
vary	O
according	O
to	O
whether	O
the	O
host	O
platform	O
is	O
Windows	B-Application
or	O
Unix-like	B-Operating_System
.	O
</s>
<s>
For	O
Perl	B-Language
there	O
are	O
several	O
modules	O
available	O
for	O
memory	O
mapping	O
files	O
on	O
the	O
CPAN	B-Language
,	O
such	O
as	O
and	O
.	O
</s>
<s>
In	O
the	O
Microsoft	O
.NET	O
runtime	O
,	O
P/Invoke	O
can	O
be	O
used	O
to	O
use	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
directly	O
through	O
the	O
Windows	B-Library
API	I-Library
.	O
</s>
<s>
Managed	O
access	O
(	O
P/Invoke	O
not	O
necessary	O
)	O
to	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
was	O
introduced	O
in	O
version	O
4	O
of	O
the	O
runtime	O
(	O
see	O
)	O
.	O
</s>
<s>
For	O
previous	O
versions	O
,	O
there	O
are	O
third-party	O
libraries	O
which	O
provide	O
managed	O
API	B-General_Concept
's	O
.	O
</s>
<s>
For	O
the	O
R	B-Language
programming	I-Language
language	I-Language
there	O
exists	O
a	O
library	O
on	O
CRAN	O
called	O
which	O
uses	O
the	O
Boost	B-Language
library	I-Language
and	O
provides	O
memory-mapped	O
backed	O
arrays	O
directly	O
in	O
R	B-Language
.	O
The	O
package	O
offers	O
memory-mapped	O
vectors	O
,	O
matrices	O
,	O
arrays	O
and	O
data	O
frames	O
.	O
</s>
<s>
The	O
J	B-Language
programming	I-Language
language	I-Language
has	O
supported	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
since	O
at	O
least	O
2005	O
.	O
</s>
<s>
Support	O
can	O
be	O
loaded	O
from	O
'	O
data/jmf	O
'	O
J	B-Language
's	O
Jdb	O
and	O
JD	O
database	O
engines	O
use	O
memory	B-General_Concept
mapped	I-General_Concept
files	I-General_Concept
for	O
column	O
stores	O
.	O
</s>
