Low level module for mmap (memory mapping of files).
Mmap.Anonymous
—TypeMmap.Anonymous(name::AbstractString="", readonly::Bool=false, create::Bool=true)
Create anIO
-like object for creating zeroed-out mmapped-memory that is not tied to a file for use inmmap
. Used bySharedArray
for creating shared memory arrays.
Examples
julia> using Mmapjulia> anon = Mmap.Anonymous();julia> isreadable(anon)truejulia> iswritable(anon)truejulia> isopen(anon)true
Mmap.mmap
—Functionmmap(io::Union{IOStream,AbstractString,Mmap.AnonymousMmap}[, type::Type{Array{T,N}}, dims, offset]; grow::Bool=true, shared::Bool=true)mmap(type::Type{Array{T,N}}, dims)
Create anArray
whose values are linked to a file, using memory-mapping. This provides a convenient way of working with data too large to fit in the computer's memory.
The type is anArray{T,N}
with a bits-type element ofT
and dimensionN
that determines how the bytes of the array are interpreted. Note that the file must be stored in binary format, and no format conversions are possible (this is a limitation of operating systems, not Julia).
dims
is a tuple or singleInteger
specifying the size or length of the array.
The file is passed via the stream argument, either as an openIOStream
or filename string. When you initialize the stream, use"r"
for a "read-only" array, and"w+"
to create a new array used to write values to disk.
If notype
argument is specified, the default isVector{UInt8}
.
Optionally, you can specify an offset (in bytes) if, for example, you want to skip over a header in the file. The default value for the offset is the current stream position for anIOStream
.
Thegrow
keyword argument specifies whether the disk file should be grown to accommodate the requested size of array (if the total file size is < requested array size). Write privileges are required to grow the file.
Theshared
keyword argument specifies whether the resultingArray
and changes made to it will be visible to other processes mapping the same file.
For example, the following code
# Create a file for mmapping# (you could alternatively use mmap to do this step, too)using MmapA = rand(1:20, 5, 30)s = open("/tmp/mmap.bin", "w+")# We'll write the dimensions of the array as the first two Ints in the filewrite(s, size(A,1))write(s, size(A,2))# Now write the datawrite(s, A)close(s)# Test by reading it back ins = open("/tmp/mmap.bin") # default is read-onlym = read(s, Int)n = read(s, Int)A2 = mmap(s, Matrix{Int}, (m,n))
creates am
-by-n
Matrix{Int}
, linked to the file associated with streams
.
A more portable file would need to encode the word size – 32 bit or 64 bit – and endianness information in the header. In practice, consider encoding binary data using standard formats like HDF5 (which can be used with memory-mapping).
mmap(io, BitArray, [dims, offset])
Create aBitArray
whose values are linked to a file, using memory-mapping; it has the same purpose, works in the same way, and has the same arguments, asmmap
, but the byte representation is different.
Examples
julia> using Mmapjulia> io = open("mmap.bin", "w+");julia> B = mmap(io, BitArray, (25,30000));julia> B[3, 4000] = true;julia> Mmap.sync!(B);julia> close(io);julia> io = open("mmap.bin", "r+");julia> C = mmap(io, BitArray, (25,30000));julia> C[3, 4000]truejulia> C[2, 4000]falsejulia> close(io)julia> rm("mmap.bin")
This creates a 25-by-30000BitArray
, linked to the file associated with streamio
.
Mmap.sync!
—FunctionMmap.sync!(array)
Forces synchronization between the in-memory version of a memory-mappedArray
orBitArray
and the on-disk version.
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.