memory mapped buffer cleaner

resource path

add bytebuffer cleaner

added comments
This commit is contained in:
Colin Surprenant 2017-02-20 15:21:53 -05:00
parent 9659db77fb
commit c63421aa16
2 changed files with 13 additions and 3 deletions

View file

@ -1,5 +1,8 @@
package org.logstash.common.io;
import sun.misc.Cleaner;
import sun.nio.ch.DirectBuffer;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
@ -103,9 +106,16 @@ public class MmapPageIO extends AbstractByteBufferPageIO {
public void close() throws IOException {
if (this.buffer != null) {
this.buffer.force();
// calling the cleaner() method releases resources held by this direct buffer which would be held until GC otherwise.
// see https://github.com/elastic/logstash/pull/6740
Cleaner cleaner = ((DirectBuffer) this.buffer).cleaner();
if (cleaner != null) { cleaner.clean(); }
}
if (this.channel != null && this.channel.isOpen()) {
this.channel.close();
if (this.channel != null) {
if (this.channel.isOpen()) { this.channel.force(false); }
this.channel.close(); // close can be called multiple times
}
this.channel = null;
this.buffer = null;

View file

@ -46,7 +46,7 @@ public class FileCheckpointIOTest {
Path fullFileName = Paths.get(checkpointFolder, "checkpoint.head");
byte[] contents = Files.readAllBytes(fullFileName);
URL url = this.getClass().getResource("checkpoint.head");
Path path = Paths.get(url.getPath());
Path path = Paths.get(url.toURI());
byte[] compare = Files.readAllBytes(path);
assertThat(contents, is(equalTo(compare)));
}