public abstract class Unmapper extends java.lang.Object
This is a tricky business. There is no good way to unmap the memory mapped by a MappedByteBuffer. The MappedByteBuffer javadocs say "A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.", and there is no explicit unmap method. However, since the resources locked by memory mapping are separate from the JVM heap, there is no guarantee that garbage collection will happen even when mapped memory reaches crisis levels, and in practice this can lead to cache thrashing and the OS locking up even when there are many MappedByteBuffers without active references, at least on some platforms (I see it on Scientific Linux 6.5 with 24Gb RAM, but not SL 6.3 with 4Gb, but I'm not sure what the relevant differences are).
The preferred method used here is to employ classes in the
sun.* namespace to do the
unmapping. This is clearly not guaranteed to work on all J2SE
implementations, and moreover it risks a JVM crash if the
buffer instance is used after the umapping has been done.
So use it WITH EXTREME CAUTION.
If the relevant classes are not available at runtime,
unmapping simply isn't done.
See also Java Bug Java Bug #4724038.
Forcing garbage collection with
System.gc() calls would
be another possibility (may be added here at some point),
but it's not guaranteed to do anything, and doing it too often
would impact performance.
|Modifier and Type||Field and Description|
Name of system property to control buffer unmapping ("startable.unmap").
|Constructor and Description|
|Modifier and Type||Method and Description|
Returns an instance of this class.
Attempts to release the resources (mapped memory) used by a given MappedByteBuffer.
public static final java.lang.String UNMAP_PROPERTY
sun": best-efforts sun.misc-based option
cleaner": sun.misc.Cleaner-based option (available in Oracle java6 through java8)
unsafe": sun.misc.Unsafe-based option (available in Oracle java9 and later?)
none": no unmapping
sun" if available, else fall back to none. You can also give the classname of an
Unmapperconcrete subclass with a no-arg constructor.
public abstract boolean unmap(java.nio.MappedByteBuffer buf)
public static Unmapper getInstance()