Omry Yadan Omry Yadan - 8 months ago 30
Java Question

Java array with more than 4gb elements

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.