public final class BufferUnmapper extends Object
MappedByteBuffer.
Java provides no public API to unmap a MappedByteBuffer. On Linux this is merely a nuisance (the OS allows replacing mapped files), but on Windows the file remains locked until the mapping is released, preventing database file replacement during updates.
Two strategies are supported, selected once at class load time based on the JVM major version:
| JDK | Strategy | JVM flags |
|---|---|---|
| 8 | DirectBuffer.cleaner().clean() | none |
| 9–16 | Not supported — throws exception | |
| 17–25 | Unsafe.invokeCleaner() |
--add-opens java.base/sun.misc=ALL-UNNAMED |
| 26 | Unsafe.invokeCleaner() |
--sun-misc-unsafe-memory-access=allow |
| 27+ | Requires rewrite to FFM Arena-based mapping | |
When Unsafe.invokeCleaner is removed (expected after JDK 26,
see JEP 471/498), the reader must be rewritten to use the FFM API's
Arena-based file mapping:
Arena arena = Arena.ofConfined(); MemorySegment seg = channel.map(READ_ONLY, 0, size, arena); // ... use seg for reads ... arena.close(); // unmaps the file immediately
There is no drop-in replacement for unmapping an existing
MappedByteBuffer after JDK 26.
Everything is via reflection so the code compiles on JDK 8+.
| Modifier and Type | Method and Description |
|---|---|
static String |
getStrategyDescription()
Returns the detected unmap strategy for diagnostic/logging purposes.
|
static void |
unmap(MappedByteBuffer buffer)
Forces immediate unmapping of a MappedByteBuffer.
|
public static void unmap(MappedByteBuffer buffer)
On success, the OS file lock is released and the underlying file can be replaced or deleted. The buffer must NOT be used after this call.
buffer - The MappedByteBuffer to unmap (must not be null).UnsupportedOperationException - if running on Java 9-16
(which is not supported for this operation).public static String getStrategyDescription()
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.