Java数组超过4GB的元素数组、元素、Java、GB

2023-09-08 09:56:01 作者:各自安好

我有一个大的文件,它的预期是大约12 GB。我想加载所有到内存结实的64位计算机具有16 GB的RAM,但我认为Java不支持字节数组那么大:

 文件f =新的文件(文件);
长尺寸= f.length();
字节的数据[] =新的字节[尺寸] //<  - 不编译,甚至没有在64位JVM
 

是否有可能与Java?

从Eclipse编译器编译错误是:

 类型不匹配:无法从长转换为int
 

javac的给出了:

 的precision可能损失
发现:长
要求:INT
         字节的数据[] =新的字节[尺寸]
 
图解 让你彻底搞懂Java数组的内存分配

解决方案

Java数组索引的数据类型为 INT (4字节或32位),所以我怕在阵列1或2147483647插槽 - 你限制在2 31 。我读出的数据到另一个数据结构中,像2D阵列

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 arrays that big:

File f = new File(file);
long size = f.length();
byte data[] = new byte[size]; // <- does not compile, not even on 64bit JVM

Is it possible with Java?

The compile error from the Eclipse compiler is:

Type mismatch: cannot convert from long to int

javac gives:

possible loss of precision
found   : long
required: int
         byte data[] = new byte[size];

解决方案

Java array indices are of type int (4 bytes or 32 bits), so I'm afraid you're limited to 231 − 1 or 2147483647 slots in your array. I'd read the data into another data structure, like a 2D array.