我正在为索引文件使用内存映射 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 或 32 位 Linux 时,我经常遇到取消映射问题,但在 64 位 Linux 生产环境中,一切似乎都可以正常工作,但文件保持原始大小。
谁能解释为什么会发生这种情况和/或如何解决问题?