0

I am little surprised to see why on my machine, the maximum size of an array is Integer.MAX_VALUE/7
I know that the arrays are indexed by integers, and so the array size cannot be greater than Integer.MAX_VALUE. I also read some stackoverflow discussions where I found that it varies on the JVM, and some(5-8 bites) are used by JVM.
In that case also, the maximum values should be Integer.MAX_VALUE-8.

Any value in between Integer.MAX_VALUE-2 and Integer.MAX_VALUE/7 gives me the error: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

int[] arr = new int[Integer.MAX_VALUE/7];

This is the largest value I can assign to array on my machine. Are specific reasons for this?

Update: I am running the code from eclipse in which the default heap size is 1024Mb. Below are the more details from my environment:

System.out.println(Runtime.getRuntime().totalMemory()/(1024*3));
System.out.println(Runtime.getRuntime().freeMemory()/(1024*3));
System.out.println(Runtime.getRuntime().maxMemory()/(1024*3));

give the output:

40618
40195
594773
The Coder
  • 3,447
  • 7
  • 46
  • 81
  • 3
    What is your maximum heap size set to? – Oliver Charlesworth Jan 20 '18 at 19:36
  • 1
    https://stackoverflow.com/questions/3038392/do-java-arrays-have-a-maximum-size – cloudwalker Jan 20 '18 at 19:41
  • @OliverCharlesworth updated the details. – The Coder Jan 20 '18 at 20:18
  • 1
    @cloudwalker I have already mentioned that link in my question. And I came to create a new question only when I didn't find that link helpful. – The Coder Jan 20 '18 at 20:19
  • 3
    Dear downvoters, it would be very soothing to my mind if anyone could explain me for your downvotes!!!! – The Coder Jan 20 '18 at 20:20
  • @puspen The down-votes may be due to not searching and studying Stack Overflow thoroughly before posting. The issues of maximum array size and memory allocation have been addressed many times already. For example, [this Answer by dnuka](https://stackoverflow.com/a/30973632/642706) on the [Question you yourself linked](https://stackoverflow.com/q/3038392/642706) addresses your specific issue: the practical limit of an array's size is available memory. – Basil Bourque Jan 21 '18 at 00:09

1 Answers1

2

As mentioned already by cloudworker, the real limits for array are explained here: Do Java arrays have a maximum size?

In your case 1GB is just not enough heap space for array that huge.

I do not know what exact processes are run in JVM, but from what I am able to count:

Integer.MAX_VALUE= ~2 billions
int = 4bytes
2billions*4bytes=8billions bytes = 8GB memory

With 1GB heapspace you should be able to have ~ /8 of MAX_VALUE. (I think that reason that you can actually get more than /8 is some optimization in JVM)

libik
  • 22,239
  • 9
  • 44
  • 87
  • I ran into a situation where I need to store around 300mb xml file. Since String is backed by a char[] only, and the maximum value a char[] can consume would be ~2billion * 2byte = 4GB. Will it be a bad practice to store such large file into String instance? – The Coder Jan 22 '18 at 08:40