java allocatedirect,JAVA NIO Bytebuffer.allocateDirect()在int上的大小限制

博主尝试创建一个10GB的Off-Heap内存缓冲区以避免Full GC导致的JVM冻结问题。然而,`ByteBuffer.allocateDirect()`方法仅支持Integer最大值。解决方案是使用多个较小的ByteBuffer实例,并在代码中自行管理这些缓冲区,通过索引来访问所需的数据。这种方法可以在不引发内存溢出的情况下实现大容量存储。
摘要由CSDN通过智能技术生成

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.

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

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

please help me.

解决方案

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.

e.g.:

ByteBuffer[] myBuffers = new ByteBuffer[howMany];

for (int x = 0; x < howMany; x++) {

myBuffers[x] = ByteBuffer.allocateDirect(prettyBig);

}

and then

byte getByte(long index) {

int bufx = index / howMany;

int bx = index % howMany;

return myBuffers[bufx].get(bx);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值