Solved

Requested array size exceeds VM limit

Posted on 2016-10-26
3
112 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

776 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