Java array with more than 4gb elements

后端 未结 11 1845
心在旅途
心在旅途 2020-11-27 07:06

I have a big file, it\'s expected to be around 12 GB. I want to load it all into memory on a beefy 64-bit machine with 16 GB RAM, but I think Java does not support byte arra

11条回答
  •  余生分开走
    2020-11-27 07:55

    You might consider using FileChannel and MappedByteBuffer to memory map the file,

    FileChannel fCh = new RandomAccessFile(file,"rw").getChannel();
    long size = fCh.size();
    ByteBuffer map = fCh.map(FileChannel.MapMode.READ_WRITE, 0, fileSize);
    

    Edit:

    Ok, I'm an idiot it looks like ByteBuffer only takes a 32-bit index as well which is odd since the size parameter to FileChannel.map is a long... But if you decide to break up the file into multiple 2Gb chunks for loading I'd still recommend memory mapped IO as there can be pretty large performance benefits. You're basically moving all IO responsibility to the OS kernel.

提交回复
热议问题