public class MMapDirectory extends FSDirectory
Directoryimplementation that uses mmap for reading, and
NOTE: memory mapping uses up a portion of the
virtual memory address space in your process equal to the
size of the file being mapped. Before using this class,
be sure your have plenty of virtual address space, e.g. by
using a 64 bit JRE, or a 32 bit JRE with indexes that are
guaranteed to fit within the address space.
On 32 bit platforms also consult
if you have problems with mmap failing because of fragmented
address space. If you get an OutOfMemoryException, it is recommended
to reduce the chunk size, until it works.
this bug in Sun's JRE, MMapDirectory's
is unable to close the underlying OS file handle. Only when GC
finally collects the underlying objects, which could be quite
some time later, will the file handle be closed.
This will consume additional transient disk usage: on Windows, attempts to delete or overwrite the files will result in an exception; on other platforms, which typically have a "delete on last close" semantics, while such operations will succeed, the bytes are still consuming space on disk. For many applications this limitation is not a problem (e.g. if you have plenty of disk space, and you don't rely on overwriting files on Windows) but it's still an important limitation to be aware of.
This class supplies the workaround mentioned in the bug report
setUseUnmap(boolean)), which may fail on
non-Sun JVMs. It forcefully unmaps the buffer on close by using
an undocumented internal cleanup functionality.
true, if the workaround
can be enabled (with no guarantees).
NOTE: Accessing this class either directly or
indirectly from a thread while it's interrupted can close the
underlying channel immediately if at the same time the thread is
blocked on IO. The channel will remain closed and subsequent access
MMapDirectory will throw a
|Modifier and Type||Field and Description|
DEFAULT_READ_CHUNK_SIZE, directory, staleFiles
|Constructor and Description|
Create a new MMapDirectory for the named location and
Create a new MMapDirectory for the named location.
|Modifier and Type||Method and Description|
Returns the current mmap chunk size.
Creates an IndexInput for the file with the given name.
Sets the maximum chunk size (default is 1 GiBytes for 64 bit JVMs and 256 MiBytes for 32 bit JVMs) used for memory mapping.
This method enables the workaround for unmapping the buffers from address space after closing
close, createOutput, deleteFile, ensureCanWrite, fileExists, fileLength, fileModified, fileModified, fsync, getDirectory, getFile, getLockID, getReadChunkSize, listAll, listAll, onIndexOutputClosed, open, open, openInput, setLockFactory, setReadChunkSize, sync, sync, toString, touchFile
clearLock, copy, copy, ensureOpen, getLockFactory, makeLock
public static final int DEFAULT_MAX_BUFF
public static final boolean UNMAP_SUPPORTED
true, if this platform supports unmapping mmapped files.
public MMapDirectory(File path, LockFactory lockFactory) throws IOException
public void setUseUnmap(boolean useUnmapHack)
IndexInput, that is mentioned in the bug report. This hack may fail on non-Sun JVMs. It forcefully unmaps the buffer on close by using an undocumented internal cleanup functionality.
NOTE: Enabling this is completely unsupported
by Java and may lead to JVM crashes if
is closed while another thread is still accessing it (SIGSEGV).
public boolean getUseUnmap()
true, if the unmap workaround is enabled.
public final void setMaxChunkSize(int maxChunkSize)
1 << 30, as the address space is big enough. Please note: This method always rounds down the chunk size to a power of 2.
public final int getMaxChunkSize()