Solved

Requested array size exceeds VM limit

Posted on 2016-10-26
3
84 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
ID: 41860389
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
ID: 41861581
>> 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
ID: 41867014
thx
0

Featured Post

Being driven mad by email signature updates?

Having to make a change to your users’ email signatures, yet again? Feel like your head is going to explode? Rely on an Exclaimer email signature management solution to make the process simple!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 45
print map entry 34 55
Apps blocked by Java 9 64
Why my table column Id is not passed to java object? 4 26
The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

895 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

12 Experts available now in Live!

Get 1:1 Help Now