0

I was wrote a little scratch program to check the time of looping through a 2 dimensional array by rows or columns, since I remembered that one way was much faster for large data sets, but I couldn't remember which way. I ran into an interesting error when I ran my program though.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at testing.ForLoopTest.main(ForLoopTest.java:10)

Now in my test I am using a int[10000][10000] running from in eclipse with the default heap settings. I know that I can increase the heap size, but my gut tells me that this should run just fine without me needing to do that. Is this normal? Is there something silly in my code that I am not seeing?

Here is my code

public static void main(String[] args){
    final int arrSize = 10000;
    long start = 0;
    long rowFirstTime = 0;
    long colFirstTime = 0;
    int[][] arr = new int[arrSize][arrSize];

    start = System.currentTimeMillis();
    for(int x = 0; x < arrSize; x++){
        for(int y = 0; y < arrSize; y++){
            arr[x][y] = -1;
        }
    }
    rowFirstTime = System.currentTimeMillis() - start;
    start = System.currentTimeMillis();
    for(int y = 0; y < arrSize; y++){
        for(int x = 0; x < arrSize; x++){
            arr[x][y] = 0;
        }
    }
    colFirstTime = System.currentTimeMillis() - start;
    System.out.println("row time is " + rowFirstTime);
    System.out.println("col time is " + colFirstTime);
}

The error happens on array initialization, it doesn't get to the for loops.

Thanks, zdevex

1
  • You ARE using 100,000,00 (100 million) ints, are you sure the default heap size will support that? Commented Aug 11, 2011 at 16:29

5 Answers 5

5

You are trying to allocate memory for 10 000 arrays of length 10 000. This requires lots of memory (about 400 MiB, by a raw calculation), which are unavailable in your JVM. This causes your OutOfMemoryError.

Try to augment the amount of memory allocated to your JVM in the execution arguments. You are looking at something like the -Xmx argument. Try giving it a value greater than 400 MiB. I think you might be able to change this value in the project properties in Eclipse.

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

4 Comments

Closer to 380 MB. But yes, I doubt the heap is that large by default.
@Grimace: Raw calculation, I don't think the exact value is interesting, a row of magnitude is enough to understand the problem :) And yes, 380 Mb won't fit anyway.
That makes sense. I guess I didn't think that through. Thanks!
The default maximum memory size is a percentage of the total physical memory in the server JVM for Java 6 . On my PC the default is 6 GB. I also use the -mx as its shorter ;)
2

10,000 * 10,000 * 4 bytes = 400,000,000

No, that 400Million Bytes won't fit inside the default heap space!

Comments

0

I think your gut feeling is wrong and that 10000 * 10000 * (size of int) bytes is bigger than your default heap size.

Comments

0

Let's see...10,000 times 10,000 = 100,000,000. Four bytes per integer gets you to 400,000,000 bytes. Plus some overhead for the 10,000 array elements. You think that allocating half a gigabyte should "run just fine" without doing anything about the heap size?

Comments

0

An int takes 4 bytes.

10000 * 10000 * 4 bytes = 400000000 bytes ~= 381 MBs. And you have to add the size of the taken by the arrays.

If your heap size is less than that, then you need to increase the heap size.

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.