我正在使用内存映射IO来处理索引文件,但问题是如果该文件大部分为空,则无法调整文件大小。
之前某处:
MappedByteBuffer map = raf.getChannel().map(MapMode.READ_WRITE, 0, 1 << 30);
raf.close();
// use map
map.force();
map = null;
调整大小:
for (int c = 0; c < 100; c++) {
RandomAccessFile raf = new RandomAccessFile(indexFile, "rw");
try {
raf.setLength(newLen);
if (c > 0) LOG.warn("used " + c + " iterations to close mapped byte buffer");
return;
} catch (Exception e) {
System.gc();
Thread.sleep(10);
System.runFinalization();
Thread.sleep(10);
} finally {
raf.close();
}
}
在使用Windows或Linux 32位时,我经常遇到取消映射问题,但在64位Linux生产环境中,一切似乎都没有警告地工作,但文件仍保留原始大小。
有人能解释一下为什么会这样发生和/或如何解决这个问题吗?