首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JVM被MappedByteBuffer杀死

JVM被MappedByteBuffer杀死
EN

Stack Overflow用户
提问于 2014-03-04 07:19:57
回答 1查看 328关注 0票数 1

我有这个函数:

代码语言:javascript
复制
    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,并显示以下错误:

代码语言:javascript
复制
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一起被杀死。

EN

回答 1

Stack Overflow用户

发布于 2014-03-04 08:13:11

MappedByteBuffer分配的VM没有明确定义的释放时间,因此永远不会释放。因此,在Java中打开大量内存映射文件是非常有问题的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22159654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档