Skip to content

Close mapped byte buffers #17

@msrocka

Description

@msrocka

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions