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,450 points
5,717 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,500 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, 2019 by Sirajul
• 59,230 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,830 points
2,884 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, 2019 in Java by Sirajul
• 59,230 points
13,069 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,220 points
4,758 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 Parth
• 4,640 points
1,210 views
+1 vote
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,620 points
1,326 views
+1 vote
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,290 points
1,185 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,220 points
3,578 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,220 points
993 views
0 votes
1 answer

Maximum depth of the java call stack

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

answered Jul 24, 2018 in Java by code.reaper12
• 3,500 points
2,221 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,500 points
4,703 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP