Solved

Requested array size exceeds VM limit

Posted on 2016-10-26
3
44 Views
Last Modified: 2016-10-31
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
0
Comment
Question by:SunnyX
3 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 100 total points
Comment Utility
I don't mean to be glib, but you can't create an array that large because the "requested array size exceeds VM limit." That is, something in the way the JVM was compiled for your machine set a maximum allowable size for an array and Integer.MAX_VALUE exceeds it. If you really need a block of integers that size, you'll have to use some other data structure.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 400 total points
Comment Utility
>> So could somebody tell me why I cannot create array with size Integer.MAX_VALUE

The max size of an array (as you figured out) is Integer.MAX_VALUE - 2, not Integer.MAX_VALUE.

I would assume this is because an array in Java includes its length at the start, so if you reserve 8 bytes for the array size, the total number of bytes allocated by an array of length MAX_VALUE-2 is MAX_VALUE.

>> The follow-up question is why can't you allocate an array of size MAX_VALUE-2 on your specific machine?

That's because you need to explicitly set the max heap size for the process to be more than 4GB, which you can do when you run the program by adding
 -Xmx5000m
to the command line when running Java (or in IntelliJ if running it inside the IDE).

That will make the max heap 5GB and you should be fine to allocate 4GB of it to your array.  (You can't set it to 4GB as you need a single 4GB block of free memory - and Java will already be using some of your heap before you start allocating things yourself - 5GB should work).

Doug
0
 

Author Closing Comment

by:SunnyX
Comment Utility
thx
0

Featured Post

Want to promote your upcoming event?

Attending an event? Speaking at a conference? Or exhibiting at a tradeshow? Easily inform your contacts by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

Join & Write a Comment

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now