-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];
    }
}
Rot-man
  • 18,045
  • 12
  • 118
  • 124
  • 4
    1 billion integers is a lot – jhamon Apr 17 '19 at 07:23
  • 3
    Possible duplicate of [java.lang.OutOfMemoryError: Java heap space while initialising an array](https://stackoverflow.com/questions/19313294/java-lang-outofmemoryerror-java-heap-space-while-initialising-an-array) – Mebin Joe Apr 17 '19 at 07: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 – Scary Wombat Apr 17 '19 at 07:24
  • 1 billions ??? you need a server to accommodate the memory bro. – Srinivasan Sekar Apr 17 '19 at 07:25
  • 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. – Jon Skeet Apr 17 '19 at 07:26
  • Agreed , but my machine has overall 4gb memory only @JonSkeet – Srinivasan Sekar Apr 17 '19 at 07:28
  • 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. – Jon Skeet Apr 17 '19 at 07:33
  • Depending on the numbers you want to store, you can consider using `short[]` instead, which should save you half the memory. – TiiJ7 Apr 17 '19 at 07:34
  • What laptop do you use? Just want to buy similar one. @JonSkeet – Srinivasan Sekar Apr 17 '19 at 07:37

5 Answers5

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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!

Kartik
  • 7,677
  • 4
  • 28
  • 50
  • 1
    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. – Jon Skeet Apr 17 '19 at 07:27
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.

Mr. Wrong
  • 500
  • 7
  • 21
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

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
Teocci
  • 7,189
  • 1
  • 50
  • 48