Solved

Requested array size exceeds VM limit

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

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

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

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

How to record audio from input sources to your PC ā€“ connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 ā€“ both 32 bit & 64.
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to ā€¦
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ā€¦

696 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