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

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

问题描述

我有一个大文件,预计大约 12 GB.我想将它全部加载到具有 16 GB RAM 的强大 64 位机器上的内存中,但我认为 Java 不支持那么大的字节数组:

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

Java 可以吗?

Eclipse 编译器的编译错误是:

The compile error from the Eclipse compiler is:

Type mismatch: cannot convert from long to int

javac 给出:

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

推荐答案

Java 数组索引属于 int 类型(4 字节或 32 位),所以恐怕你仅限于 231 - 阵列中有 1 个或 2147483647 个插槽.我会将数据读入另一个数据结构,如二维数组.

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.

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

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