JAVA NIO Bytebuffer.allocateDirect()在int上的大小限制 [英] JAVA NIO Bytebuffer.allocateDirect() size limit over the int

查看:369
本文介绍了JAVA NIO Bytebuffer.allocateDirect()在int上的大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作堆外内存缓冲区.我想要很大的缓冲区(例如10GB). 我听说jvm堆有时会因为完整的GC而冻结.因此,我尝试使用java.nio.ByteBuffer进行缓冲.

I am trying to make off-heap memory buffer. I want very big size(like 10GB) buffer. I heard that jvm heap sometimes freeze because full GC. So, I try to make buffer with java.nio.ByteBuffer.

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

But, I met great difficulties!

java.nio.ByteBuffer.allocateDirect(int size)

功能仅支持整数.但我想要更大的尺寸.我能做些什么?我该怎么办?请帮助我堆叠溢出专家.

function only support integer. but I want bigger size. what can I do? what should I do? please help me stack overflow gurus.

我的开发环境是macbook pro,i7 2.4GHz,16GB ddr3、250sd,osx 10.9,eclipse kepler x64.

my development environment is macbook pro, i7 2.4ghz, 16gb ddr3, 250ssd, osx 10.9, eclipse kepler x64.

我尝试解决问题的方法:

I try something for solve problem:

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

but this is not works. only allocate memory 1024*1024*2000 again

请帮助我.

推荐答案

您不能使单个缓冲区那么大.时期.您可以制作几个较小的代码,然后根据需要在自己的代码中进行选择.这就是您所能做的.

You can't make a single buffer that big. Period. You can make several smaller ones and select amongst them as you need to in your own code. That's all you can do.

例如:

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); 
}

这篇关于JAVA NIO Bytebuffer.allocateDirect()在int上的大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆