olca-modules
olca-modules copied to clipboard
Close mapped byte buffers
We use mapped byte buffers when we dump matrices into files. However, there
is currently no way to close a MappedByteBuffer
via public API. On Windows
this means that the file keeps open and cannot be deleted, renamed, etc. The
mapped byte buffer is closed when it is garbage collected so we could try to
invoke garbage collection after mapping or use the internal Cleaner
API.
For example, this will fail on Windows:
@Test
public void testDeleteTempFile() throws Exception {
File file = Files.createTempFile("_file_del_test", ".txt").toFile();
try (RandomAccessFile f = new RandomAccessFile(file, "rw");
FileChannel chan = f.getChannel()) {
MappedByteBuffer buf = chan.map(
FileChannel.MapMode.READ_WRITE, 0, 42);
for (int i = 0; i < 42; i++) {
buf.put((byte) 42);
}
buf.force();
}
Assert.assertTrue(file.delete());
}
Adding a Cleaner
call currently works but it is internal API and it seems
that this was removed in Java 9:
// ...
buf.force();
Cleaner cleaner = ((sun.nio.ch.DirectBuffer) buf).cleaner();
if (cleaner != null) {
cleaner.clean();
}
// ...
This issue is just for tracking this as there seem to be no real solution to this problem available currently.
References:
- https://bugs.openjdk.java.net/browse/JDK-6558368
- https://bugs.openjdk.java.net/browse/JDK-4724038
- https://stackoverflow.com/questions/25238110/how-to-properly-close-mappedbytebuffer