?
Solved

Requested array size exceeds VM limit

Posted on 2016-10-26
3
Medium Priority
?
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 400 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 28

Accepted Solution

by:
dpearson earned 1600 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
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.
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…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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