【问题标题】:JAVA NIO Bytebuffer.allocateDirect() size limit over the intJAVA NIO Bytebuffer.allocateDirect() 大小限制超过 int
【发布时间】:2014-07-24 11:34:50
【问题描述】:

我正在尝试制作堆外内存缓冲区。我想要非常大的缓冲区(比如 10GB)。 我听说 jvm heap 有时会因为 full GC 而冻结。所以,我尝试用 java.nio.ByteBuffer 做缓冲区。

但是,我遇到了很大的困难!

java.nio.ByteBuffer.allocateDirect(int size)

函数只支持整数。但我想要更大的尺寸。我能做些什么?我该怎么办?请帮我堆叠溢出大师。

我的开发环境是macbook pro, i7 2.4ghz, 16gb ddr3, 250ssd, osx 10.9, eclipse kepler x64。

我尝试解决问题:

ByteBuffer buffer = ByteBuffer.allocateDirect(1024*1024*2000);
ByteBuffer buffer1 = ByteBuffer.allocateDirect(1024*1024*2000);
ByteBuffer buffer2 = ByteBuffer.allocateDirect(1024*1024*2000);
ByteBuffer buffer3 = ByteBuffer.allocateDirect(1024*1024*2000);

但这行不通。只再分配内存1024*1024*2000

请帮帮我。

【问题讨论】:

    标签: java nio bytebuffer


    【解决方案1】:

    你不能把一个缓冲区做得那么大。时期。您可以制作几个较小的,并根据需要在您自己的代码中选择它们。这就是你所能做的。

    例如:

    ByteBuffer[] myBuffers = new ByteBuffer[howMany];
    for (int x = 0; x < howMany; x++) {
        myBuffers[x] = ByteBuffer.allocateDirect(prettyBig);
    }
    

    然后

    byte getByte(long index) {
        int bufx = index / howMany;
        int bx = index % howMany;
        return myBuffers[bufx].get(bx); 
    }
    

    【讨论】:

    • 所以你的意思是如果我多次制作小缓冲区,总大小可以超过 2^31 吗?当我多次分配时,我看不到内存增加。我能怎么做?你能给我看一些sn-ps的代码吗?
    • 请注意,您可能需要使用 -XX:MaxDirectMemorySize 来调整 jvm 允许分配给直接字节缓冲区的内存量。默认行为因版本、供应商、架构和其他选项而异。
    • 你认为为什么需要这么大的缓冲区?
    猜你喜欢
    • 2012-10-25
    • 2011-09-28
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2021-06-12
    相关资源
    最近更新 更多