tags:

views:

279

answers:

3

Is there a limit for an array in Java?

Thanks

+10  A: 

Yes, since the index is int based, the maximum length is equal to Integer.MAX_VALUE: 2147483647. It's however a lot, you may hit the OutOfMemoryError sooner than the array size limit.

BalusC
Although you could add extra dimensions...
James
@James - a parent array whose indices happen to reference child arrays does not make the parent array any bigger
Matt
Might be good to mention that in most practical cases, the maximum array size is determined by the maximum Java heap size. On a 32-bit JVM, it's impossible to allocate any array of `Integer.MAX_VALUE` elements.
Daniel Pryden
@Daniel I believe you can, although you will need a process size over 2 GB (Windows has some strange limitations that I forget the details of).
Tom Hawtin - tackline
@Daniel: That's not true. A byte array with Integer.MAX_VALUE elements requires appr. 2GB of heap, which is possible on 32-bit operating systems, which allow >2GB of per-process memory - e.g. Solaris.
jarnbjo
(In the interests of obscurity, JavaCard 1.0 used `short` for array indexes.)
Tom Hawtin - tackline
@Tom: Windows has indeed a default process size limit or 2GB, even in 64bit. See [Microsoft specs](http://msdn.microsoft.com/en-us/library/aa366778%28VS.85%29.aspx).
BalusC
@jarnbjo: Point taken. (I guess the same would be true on Win32 with the /3GB switch, no?) But even on an OS that allows more than 2GB process space, the JVM itself, any native heap allocations and any loaded native libraries also eat into the virtual address space, so it still may not be possible to get 2GB of continuous space.
Daniel Pryden
@BalusC: In the specs you linked to, it is stated that for 64 bit processes on 64 bit Windows, the default is 8TB. Only 32 bit processes get 2GB by default, which is the same behaviour as in 32 bit Windows.
OregonGhost
@Oregon: yes, you're right, I looked in the wrong row!
BalusC
@Daniel: Java arrays do not necessarily require continuous memory. I'm not sure though, if any VM actually implements array storage in non-continuous memory space, but the G1 garbage collector supports heap defragmentation.
jarnbjo
@BalusC According to that page with some flag set it will go to 3 GB (32-bit) or 4 GB (64-bit). IIRC, there is some problem with some DLLs requiring specific address locations.
Tom Hawtin - tackline
+3  A: 

There are actually two limits. One, the maximum element indexable for the array and, two, the amount of memory available to your application. Depending on the amount of memory available and the amount used by other data structures, you may hit the memory limit before you reach the maximum addressable array element.

tvanfosson
+1  A: 

Haven't seen the right answer, even though it's very easy to test.

In a recent HotSpot VM, the correct answer is Integer.MAX_VALUE - 5. Once you go beyond that:

public class Foo {
  public static void main(String[] args) {
    Object[] array = new Object[Integer.MAX_VALUE - 4];
  }
}

You get:

Exception in thread "main" java.lang.OutOfMemoryError:
  Requested array size exceeds VM limit
Kevin Bourrillion
As pointed out, that depends on amount of (default) allocated memory for the JVM. Mine in Eclipse for example already errors on `Integer.MAX_VALUE / 2`. Your downvotes were honestly said ridiculous and nitpicky :)
BalusC
I think the idea of downvotes makes no sense unless we are willing to downvote answers that are plain and simply _wrong_. Does the difference of five bytes actually matter in the real world, NO, of course not. But it concerns me that people are willing to give an answer "authoritatively" without even trying it to see if it really works.As for the memory limit, well, DUH. That's like if you asked me "how many grapes can you eat?" and I said "well, it depends on how many I have in the fridge at the time."
Kevin Bourrillion