Link to home
Start Free TrialLog in
Avatar of SunnyX
SunnyX

asked on

Requested array size exceeds VM limit

Let say I'm trying to create array with size of Integer.MAX_VALUE
 public static void main(String[] args) {
        int[] ints = new int[Integer.MAX_VALUE]; 
    }

Open in new window

However, I get an error :
"java.lang.OutOfMemoryError: Requested array size exceeds VM limit"

Because I use the Win10, I thought that this is because Win10 max limit for applicaiton is 2 Gb , but in order to create array with size Integer.MAX_VALUE I would need 4Gb space. ( 2^32 = 4 * 1024 * 1024 * 1024 )


However, when I tried to make   int[] ar = rndArray(Integer.MAX_VALUE -2 );
I get "java.lang.OutOfMemoryError: Java heap space" - this error probably go from intellij idea that I use which is limit heap memory size 128 Mb
So I believe my assuption is failed, because Integer.MAX_VALUE -2 far more than 128 Mb

It is also not bacause my JMV in 32bit mode bacause I check my java -version
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
and JMV 32bit has ~1,3Gb limit

So could somebody tell me why I cannot create array with size Integer.MAX_VALUE

P.S. I have 8.0 DDR3 RAM overall. The Win10 RAM monitor show me that I've avaliable 4.2Gb RAM
SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SunnyX
SunnyX

ASKER

thx