Java数组超过4GB的元素 [英] Java array with more than 4gb elements

查看:136
本文介绍了Java数组超过4GB的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

是否有可能与Java?

从Eclipse编译器编译错误是:

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

javac的给出了:

 的precision可能损失
发现:长
要求:INT
         字节的数据[] =新的字节[尺寸]
 

解决方案

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.

这篇关于Java数组超过4GB的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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