How long is the maximum length of a Java integer array?

Source: Internet
Author: User

How long is the maximum length of a Java integer array?

Today on the Internet to check a bit, all kinds of statements have, this problem seems to always bother us Java beginner, helpless, had to try for a while, the following is my test code, if there are errors, but also look at the liberal enlighten!

Using Eclipse's default VM arguments, run the following code:

public static void Main (string[] args) {
Byte[] Array=new byte[61858764];
Short[] Shortarray=new short[30929382];
Int[] Intarray=new int[15464691];
Long[] Longarray=new long[7732345];
}

Success, no error, I know under Eclipse default VM arguments, the maximum length of the byte array is 61858764, which will be nearly 60M.

Since short is one-fold longer than byte, the maximum length of the short array is 30929382, which is half the maximum length of the byte array,

And so on, the maximum length of the int array is the 15464691,long array maximum length is 7732345

I thought this was the end result, and then it suddenly occurred to me to change the maximum memory of the virtual machine, is this the result?

I modified the Eclipse VM arguments setting to:-xms256m-xmx768m

Test the code again and find that the maximum length of the byte array can be longer,

The Windows Task Manager shows that the Javaw.exe process running the code consumes more memory (nonsense, hehe)

To this end, it should be concluded that by modifying the maximum memory for a virtual machine, the maximum length of a Java integer array can be very long, but how long? (definitely smaller than memory)

I tried to define a byte array with a length of 618587640, would be nearly 600M, no error, the longer array seemed meaningless, and I did not test.

PS: The above test, I just define such a long array, and do not operate on these arrays, so I do not guarantee the actual operation of the array without error (I lazy)

And, I'm just testing an array of integers, not testing other types of arrays, and I'm not sure whether other types of arrays also fit this conclusion (and lazy)

Should be related to memory, array length parameter is int, so theoretically the maximum number is Integer.max_value

How long is the maximum length of a Java integer array?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.