public abstract class Unmapper extends 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
implementation-specific 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 |
---|---|
static String |
UNMAP_PROPERTY
Name of system property to control buffer unmapping ("startable.unmap").
|
Constructor and Description |
---|
Unmapper() |
Modifier and Type | Method and Description |
---|---|
static Unmapper |
getInstance()
Returns an instance of this class.
|
abstract boolean |
unmap(MappedByteBuffer buf)
Attempts to release the resources (mapped memory) used by a given
MappedByteBuffer.
|
public static final String UNMAP_PROPERTY
sun
": best-efforts sun.misc-based optioncleaner
": 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 unmappingsun
" if available,
else fall back to none.
You can also give the classname of an Unmapper
concrete subclass with a no-arg constructor.public abstract boolean unmap(MappedByteBuffer buf)
buf
- bufferpublic static Unmapper getInstance()
Copyright © 2024 Central Laboratory of the Research Councils. All Rights Reserved.