我有这个函数:
private double[] readTableDouble(final RandomAccessFile file, final long startIndex, final int length) throws IOException {
double[] doubles;
try (FileChannel channel = file.getChannel()) {
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, startIndex * 8, length * 8);
int byteIndex = buffer.limit() - 8;
doubles = new double[length];
int doubleIndex = 0;
while (byteIndex > 0 && doubleIndex < length) {
long l = 0;
for (int i = 0; i < 8; i++) {
l |= ((long) (buffer.get(byteIndex + i) & 0xFF)) << i * 8;
}
byteIndex -= 8;
if (l != fillerLong) {
doubles[doubleIndex++] = Double.longBitsToDouble(l);
}
}
if (doubleIndex == length) {
file.close();
channel.close();
return doubles;
}
// try again from the start if not enough numbers
buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, (startIndex + length) * 8);
byteIndex = buffer.limit() - 8;
doubleIndex = 0;
while (byteIndex > 0 && doubleIndex < length) {
long l = 0;
for (int i = 0; i < 8; i++) {
l |= ((long) (buffer.get(byteIndex + i) & 0xFF)) << i * 8;
}
byteIndex -= 8;
if (l != fillerLong) {
doubles[doubleIndex++] = Double.longBitsToDouble(l);
}
}
}
file.close();
return doubles;
}由于某种原因,它会终止JVM,并显示以下错误:
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x000000077f280000, 13107200, 0) failed; error='Cannot allocate memory' (errno=12)
There is insufficient memory for the Java Runtime Environment to continue.
Native memory allocation (malloc) failed to allocate 13107200 bytes for committing reserved memory.
An error report file with more information is saved as:
/home/lukasz/Documents/NetBeansProjects/PPP/hs_err_pid8817.log
Java Result: 1这个函数被调用了几十万次,但是返回的缓冲区很快就被释放了。无多线程,文件大小约为340kB。
你知道为什么吗?分析器和JVM一起被杀死。
发布于 2014-03-04 08:13:11
MappedByteBuffer分配的VM没有明确定义的释放时间,因此永远不会释放。因此,在Java中打开大量内存映射文件是非常有问题的。
https://stackoverflow.com/questions/22159654
复制相似问题