0

everyone. I am working on a project. Along the way, I got the "OutOfMemory" problem with this line of code...

this.A = new int[n][n];

...for n = 10000;

I tried fixing it by adding -Xmx2048m command but the thing is when added, all of the rest of running programs stops responding. Any other suggestion for my case? Thanks a lot

Adam Arold
30.7k25 gold badges120 silver badges225 bronze badges
asked Apr 9, 2014 at 11:33
6
  • 2
    Try getting a better computer? Commented Apr 9, 2014 at 11:34
  • How much physical RAM does your computer have? Commented Apr 9, 2014 at 11:38
  • 2
    10,000 squared is 100,000,000. That makes an estimated 400 MB array size, still much lower than your 2 GB heap limit. Seems like you have other memory issues as well. Commented Apr 9, 2014 at 11:39
  • 2
    Could you post your code? Commented Apr 9, 2014 at 11:40
  • What was your original setting for -Xmx? Commented Apr 9, 2014 at 11:49

3 Answers 3

2

You have to calculate the space that array needs. If it is over 2048M then you'll receive an OutOfMemoryError.

In your case you try to allocate an array of 10000 x 10000 which is 100.000.000.

A primitive int occupies 4 bytes. This means that the whole array takes

100.000.000 * 4 bytes which is 0,37 GB of space.

So it seems that there is something else in your program which caueses the error. For example if you try to allocate multiple arrays in a loop then you can run out of memory real quick.

It can be a problem if your hardware does not have 2048M of memory.

It is also possible that before using -Xmx2048m you had for example -Xmx512m which might be too small for your array.

answered Apr 9, 2014 at 11:35

1 Comment

Do you want to say a few words about why 2048M specifically?
0

Use this:

Increase heap size in Java

-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size

java -Xms16m -Xmx64m ClassName

answered Apr 9, 2014 at 11:45

1 Comment

Try to change the number to 1000 and see if this is your problem
0

Let's see...

An int occupies 32bit or 4 bytes in memory.
Your 2-dimensional array thus requires 10000 * 10000 * 4 bytes = 400000000 bytes = 381.47 MB (plus storage for the array objects)

Let's verify that with a simple test:

int n = 10000;
int[][] foo = new int[n][n];
System.out.println("USED MEMORY: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024);

Result: 384 MB (on my machine, your results may vary).

Running out of memory in the heap happens when a requested block of memory cannot be allocated, not necessarily when the memory is full.

As you allocate memory in blocks of 40'000 bytes, you can run out of memory when no contiguous free memory of that size is available (hundreds of blocks with less that 40'000 won't help here). That means you can easily run out of memory when the free memory on the heap is fragmented as small memory blocks.

If you happen to allocate that memory multiple times in a row, try reusing the allocated memory instead of allocating it again (to prevent fragmentation to a certain degree). Alternatively, try using smaller arrays (even if it means using more of them).

answered Apr 9, 2014 at 11:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.