-2

The Maximum allowed int data type array range is 2147483647. But I am getting

Runtime Error: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at Test.main(Test.java:3)

Can someone please explain me the memory representation for this size and why JVM raising a runtime error for this?

Code:

class Test{
    public static void main(String[] args){
        int [] x = new int[1000000000];
    }
}
9
  • 4
    1 billion integers is a lot Commented Apr 17, 2019 at 7:23
  • 3
    Possible duplicate of java.lang.OutOfMemoryError: Java heap space while initialising an array Commented Apr 17, 2019 at 7:24
  • 2
    what is the size on an int, then multiply that by 1000000000, then ensure that you have this much memory, and ensure that you have this much heap Commented Apr 17, 2019 at 7:24
  • 4
    @SrinivasanSekar: It's trying to allocate just less than 4GB. The JVM apparently can't handle that, but it's hardly an enormous amount of memory. Commented Apr 17, 2019 at 7:26
  • 1
    @SrinivasanSekar: Yours may do - my laptop has 16GB and copes with this code just fine. No need for a server - just a machine with enough memory and a JVM that is able to handle it. Commented Apr 17, 2019 at 7:33

5 Answers 5

3

You're hitting a limit of the particular JVM you're using, or your system memory.

You're trying to allocate an array which will take up ~4GB of contiguous memory. I'd expect a 64-bit JVM on a machine with enough memory to handle that without any problems, but different JVMs may have different limitations. For example, I'd expect any 32-bit JVM to struggle with that.

The good news is that allocating an array that large is almost never required in regular programming. If you do need it, you'll need to make sure you work in an environment that supports it.

Even on my machine that can handle your example, if I increase the size further I get one of two errors, either the one you got:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

or for 2147483646 or 2147483647:

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

(The first happens somewhere between 1064000000 and 1065000000 on my machine.)

Sign up to request clarification or add additional context in comments.

Comments

1

By default, the int data type is a 32-bit signed two's complement integer. You declare an array of one billion of those.

You can read more about primitive datatypes at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

You could look into increasing the available memory (see Increase heap size in Java) , or look into using the Collections API, which might suit your needs better than a primitive array.

Comments

1

You mentioned the biggest number that can be stored in an int i.e. 2147483647.

But you are creating an array of a billion ints. These are two different things.

Basically you are taking up 1000000000 * 4 bytes, because size of one int is 4 bytes. That makes about 4GB of memory!

1 Comment

Why "of course"? I don't think it's unreasonable to hope that a modern JVM would be able to handle arrays of that size. 4GB is not an outrageous amount of memory to try to allocate, IMO.
1

Usually, this error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. "int[1000000000]" is consuming a lot of Java heap space and when all of the available memory in the heap region is filled and Garbage Collection is not able to clean it, the java.lang.OutOfMemoryError:Java heap space is thrown. 4GB is a lot of space

Comments

1

I think this is and old question.. well I will explain this:

Your Java code runs on a Java Virtual Machine (JVM) this machine has some limitations by default that is why you get this:

int[] array : 4 byte
0 l=1048576 s=4mb
1 l=2097152 s=8mb
java.lang.OutOfMemoryError: Java heap space l=4194304 s=16mb

How can you "fix" it on your environment?

Well easy just run your app adding a VM flag -Xmx12g.

Note:

Remember that, in practice, a JVM array size is limited by its internal representation. In the GC code, JVM passes around the size of an array in heap words as an int then converts back from heap words to jint this may cause an overflow. So in order to avoid crashes and unexpected behavior the maximum array length is limited by this max size - header size. Where header size depends on which C/C++ compiler was used to build the JVM you are running (i.e. gcc for linux, clang for macos), and runtime settings (i.e. UseCompressedClassPointers). For example, on my Linux environment the limits are:

Java HotSpot(TM) 64-Bit Server VM 1.6.0_45 limit Integer.MAX_VALUE
Java HotSpot(TM) 64-Bit Server VM 1.7.0_72 limit Integer.MAX_VALUE-1
Java HotSpot(TM) 64-Bit Server VM 1.8.0_40 limit Integer.MAX_VALUE-2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.