Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close mapped byte buffers #17

Open
msrocka opened this issue Dec 11, 2019 · 0 comments
Open

Close mapped byte buffers #17

msrocka opened this issue Dec 11, 2019 · 0 comments

Comments

@msrocka
Copy link
Member

msrocka commented Dec 11, 2019

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:

msrocka added a commit that referenced this issue Dec 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant