Java OutOfMemoryError even when structured correctly?

I am working with 2D arrays int

in Java that are square and can contain ~ 30,000 elements per row and column, which means there are 30,000 ^ 2 * 4 bytes in the array, which is less than 5 GB (and I have more than 5 GB of memory).

My basic program structure is this:

public class A {
    public static void main(String[] args) {
        Graph g = ...; // read from file
        System.out.println(B.computeSomethingBig(g));
    }
}

public class B {
     public static computeSomethingBig(Graph g) {
         int numVertices = g.numVertices();
         System.out.println(numVertices); // ~30000 maximum
         int[][] array = new int[numVertices][numVertices];
         // ... other computations
     }
}

      

Now, in Eclipse, I run main

in the class A

with these arguments:

-Xms5g -Xmx10g

      

I have it numVertices

printing a value around 30000 and setting the minimum heap size ( -Xms

) seems more than necessary. However, I get:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

      

+3


source to share


2 answers


Doing the following:

public class A {
        public static void main(String[] args) {
                int numVertices = 30000;
                int[][] array = new int[numVertices][numVertices];
        }
}

      



without any parameters, i.e. java A

results in an OOM error. In progress java -Xms5g -Xmx10g A

. I suspect the numVertices are larger than you expected. Why don't you output it before distribution to be sure.

Also note that yours Graph

or other objects in the application can use a bunch of scope as well.

+3


source


It seems to you that a separate area within the heap (such as eden, survivor, or old) is large enough to house the data structure you are trying to allocate. For example, your default 5G heap might be split like this (note that this is platform dependent):

  • Eden 1.5G
  • Two survival spaces 0.25G each
  • Old 3G


A finer grained configuration by adjusting the relationships of different areas will help you figure it out.

+1


source







All Articles