3

Error in this program for int array Can anyone explain why these two cases behave differently?

class MainOutOfMemoryError {
    /*
    case1:doesn't give me any error
    static final int s = 1024 * 1024 * 1024 * 1024 * 1024;

    public static void main(String[] args) {
        // we cant declare local variables as static
        int[] i = new int[s];
        System.out.println(s);
    }
    */

    // case2:gives error    
    static final int SIZE = 2 * 1024 * 1024;
    public static void main(String[] a) {
        int[] i = new int[SIZE];
        System.out.println(SIZE);
    }
}
4
  • Why would allocating an array of 2 million ints give an OutOfMemoryError? That's only 8 MB. Commented Oct 8, 2015 at 17:25
  • in Case1,it is showing sopln(s) prints 0 but in case 2 it prints actual Size ,WHY ? Commented Oct 8, 2015 at 17:29
  • Which case really gives an error? In your code snippet the case 2 shouldn't give any error. Commented Oct 8, 2015 at 17:30
  • @RameshRaj Then that's not an out of memory. Could you please edit your post with your comment as title. That is the actual problem. Commented Oct 8, 2015 at 17:40

4 Answers 4

3

From your comment

in Case1,it is showing sopln(s) prints 0 but in case 2 it prints actual Size ,WHY ?

In your first case, integer overflow happened and the result of s is 0

static final int s = 1024 * 1024 * 1024 * 1024 * 1024;

// s value is 0 because of overflow 

That is kind of writing

int[] i = new int[0];   

Where as in second case the result s is 2097152 a valid integer and you run out of memory while allocation of memory for integers in array.

So you are trying to do

int[] i = new int[2097152];  

Which try to allocate the memory 67108864 bits.

I'm kind of clues here that, what makes you out of memory since that bits are equals to 8.388608MB

Sign up to request clarification or add additional context in comments.

Comments

1

The int 1024 * 1024 * 1024 * 1024 * 1024 is actually 0 due to int overflow.

int arithmetic works modulo 2^32. Since 1024 = 2^10, the true mathematical value of that product is 2^50. Since 50 > 31, s == 0.

In the first case you are allocating an array of length 0. In the second case it has length over 2 million.

4 Comments

Thank you for not making it seem like 0 is the magical overflow value! If the OP had set it to 1025*1025*1025*1025*1025 it would have come out completely different ...
@dcsohl Actually my original answer did imply that. I did a quick edit before the downvotes came in!
I saw. :) But everybody else is saying "it overflowed so it came out to 0" without explaining why. Just wanted to thank you for that.
It still could get misinterpreted. It’s not only that 50 > 31, but also that 2⁵⁰ / 2³² is exactly 2⁽⁵⁰⁻³²⁾, no remainder, thus 2⁵⁰ % 2³² == 0
0

For your first example, 1024 * 1024 * 1024 * 1024 * 1024, Google calculator gives 1.1258999e+15. The Java Primitive Data Types tutorial says (in part)

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1

According to Google calculator, the maximum value of an int is thus 2147483647. Which means your value overflows int. That's why it worked.

If you test it like,

int i = 1024 * 1024 * 1024 * 1024 * 1024;
System.out.println(i);

You'll see that the result is 0.

Comments

0

I think you mean the opposite: case 1 gives an error and the 2nd doesn't:

It is not the array that is causing the memory overflow but the variable SIZE which is an int with a max value of around 2 billion, change it to long and the problem is solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.