Maximum size of an Java array

0 votes

What is the maximum number of elements that an array in Java can contain?

Oct 31, 2018 in Java by misc.edu04
• 1,420 points
675 views

2 answers to this question.

0 votes

Well, you can check it by using Integer.MAX_VALUE - 5.

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

It will throw an exception saying:

Exception in thread "main" java.lang.OutOfMemoryError:
  Requested array size exceeds VM limit
answered Oct 31, 2018 by code.reaper12
• 3,450 points
0 votes

There are actually two limits. 

  1. The maximum element indexable for the array and, 

  2. 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.

answered Aug 20 by Sirajul
• 38,420 points

Related Questions In Java

0 votes
1 answer

What is the maximum Java heap size of a 32-bit JVM on a 64-bit OS?

32-bit JVMs which expect to have a ...READ MORE

answered Nov 15, 2018 in Java by Frankie
• 9,810 points
620 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19 in Java by Sirajul
• 38,420 points
183 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
226 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by parth295
• 4,640 points
82 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
78 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
68 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
597 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
61 views
0 votes
1 answer

Maximum depth of the java call stack

Well, there is no definite depth. It ...READ MORE

answered Jul 23, 2018 in Java by code.reaper12
• 3,450 points
103 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,450 points
107 views