File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java array memory allocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java array memory allocation" Watch "Java array memory allocation" New topic

Java array memory allocation

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97

What will happen when you do

Or in other words can we allocate such large arrays.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

If there's enough memory available, yes. You may have to use Sun's -Xmx switch (or another vendor-specific mechanism) to set the max size of the Java heap.

[Jess in Action][AskingGoodQuestions]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873

The last I remember I had a question along similar lines and Jim Yingst pointed that as array object have length field which is of type 'int' we can only create arrays of len Integer.MAX_VALUE. Again this is true only if we have sufficient memory to create Integer.MAX_VALUE size array.

I ran the following,

and got,
Exception in thread "main"

[ March 04, 2005: Message edited by: Maulin Vasavada ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Maulin Vasavada:
byte[] b = new byte[Integer.MAX_VALUE];
Yes well, not everyone has 2GB of RAM devoted exclusively to the JVM!
I agree. Here's the link:
subject: Java array memory allocation
It's not a secret anymore!