Meaningless Drivel is fun!
The moose likes I/O and Streams and the fly likes Max Size of Byte Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Max Size of Byte Array?" Watch "Max Size of Byte Array?" New topic

Max Size of Byte Array?

Shaun Mc

Joined: Sep 28, 2005
Posts: 4
In my application I read in a file to a byte array:

The app works fine when i use small files, but fails with the following error, when I read in a big file (20mb or so), (I HAVE to read the file into the byte array).

Is there a limit to the size a byte array can grow??

Does anybody know a solution to the problem?


My code is:

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1076677835"

InputStream is = new FileInputStream(fileName);
long length = spssfile.length();
byte[] bytes = new byte[(int)length];, 0, bytes.length);
Joe Ess

Joined: Oct 29, 2001
Posts: 9191

Since the Java Language Specification says the argument to an array creation expression is an int and the largest int value is 2,147,483,647, I'd say that is the largest allowable array size. Are you certain that length == bytes.length? Your downcast from long to int could lose the most significant digit.

[How To Ask Questions On JavaRanch]
Henry Wong

Joined: Sep 28, 2004
Posts: 20538

If the files are supposed to be only 20 MB, why do you have indexes as high as 1 GB? Maybe there is a size miscalculation somewhere.

In any case, 1 GB devoted to a single byte array is a bit ridiculous. Memory may be cheap, but it aint that cheap yet.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 696
What line of code do you get the error on?
What are the values you are working with? What is the value of length?
Add some println()s to show them.
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Maybe you should load the file into the byte array in parts. Basically the buffer "byte []" can be flushed after loading say, every 256K of the file. I faced a similar problem sometime back too and flushing the byte array solved it.
I agree. Here's the link:
subject: Max Size of Byte Array?
jQuery in Action, 3rd edition