Java 中的内存映射大文件
Memory-mapping huge files in Java
是否可以在 Java 中对大文件(多个 GB)进行内存映射?
FileChannel
的这种方法看起来很有希望:
MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
position
和 size
都允许 64 位值——到目前为止,还不错。
MappedByteBuffer
,然而,只提供了32位位置的方法(get(int index)
,position(int newPosition)
等),这似乎暗示我无法映射大于2的文件国标.
我怎样才能绕过这个限制?
查看 Using a memory mapped file for a huge matrix 代码,它展示了如何创建 MappedByteBuffer
的列表,每个小于 2 GB,以映射整个文件:
private static final int MAPPING_SIZE = 1 << 30;
...
long size = 8L * width * height;
for (long offset = 0; offset < size; offset += MAPPING_SIZE) {
long size2 = Math.min(size - offset, MAPPING_SIZE);
mappings.add(raf.getChannel().map(FileChannel.MapMode.READ_WRITE, offset, size2));
}
根据 JDK-6347833 (fs) Enhance MappedByteBuffer to support sizes >2GB on 64 bit platforms 2 GB 限制的原因是:
A MappedByteBuffer is a ByteBuffer with additional operations to support memory-mapped file regions. To support mapping a region larger than Integer.MAX_VALUE would require a parallel hierarchy of classes. For now the only solution is create multiple MappedByteBuffers where each corresponds to a region that is no larger than 2GB.
如前所述,由于使用整数 index/position 指针,MappedByteBuffer
有 2GB 的限制。
要解决这个问题,您可以使用替代实现,例如 larray
是否可以在 Java 中对大文件(多个 GB)进行内存映射?
FileChannel
的这种方法看起来很有希望:
MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
position
和 size
都允许 64 位值——到目前为止,还不错。
MappedByteBuffer
,然而,只提供了32位位置的方法(get(int index)
,position(int newPosition)
等),这似乎暗示我无法映射大于2的文件国标.
我怎样才能绕过这个限制?
查看 Using a memory mapped file for a huge matrix 代码,它展示了如何创建 MappedByteBuffer
的列表,每个小于 2 GB,以映射整个文件:
private static final int MAPPING_SIZE = 1 << 30;
...
long size = 8L * width * height;
for (long offset = 0; offset < size; offset += MAPPING_SIZE) {
long size2 = Math.min(size - offset, MAPPING_SIZE);
mappings.add(raf.getChannel().map(FileChannel.MapMode.READ_WRITE, offset, size2));
}
根据 JDK-6347833 (fs) Enhance MappedByteBuffer to support sizes >2GB on 64 bit platforms 2 GB 限制的原因是:
A MappedByteBuffer is a ByteBuffer with additional operations to support memory-mapped file regions. To support mapping a region larger than Integer.MAX_VALUE would require a parallel hierarchy of classes. For now the only solution is create multiple MappedByteBuffers where each corresponds to a region that is no larger than 2GB.
如前所述,由于使用整数 index/position 指针,MappedByteBuffer
有 2GB 的限制。
要解决这个问题,您可以使用替代实现,例如 larray