Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OutOfMemoryError with ArrayList<List<Integer>>

I want to create a very large graph (with ~10 million edges) in Java. I plan to List<List<Integer>> to describe the edges, with the inside List<Integer> describing the two vertices of each edge (and the vertices are Integer type).

The following code throws the OutOfMemoryError after about 1 million edges are added to the graph. (I simplified how the edge is generated for the sake of discussion.)

public static void main(String[] args) {
  List<List<Integer>> graph = new ArrayList<List<Integer>>();
  for (int i = 0; i < 10000000; i++) {
    List<Integer> edge = new ArrayList<Integer>();
    // the real edges are more complicated (than from vertex i to vertex i+1)
    // this is simplified for the sake of the discussion here
    edge.add(i);
    edge.add(i+1);
    graph.add(edge);
  }
}

I have searched for OutOfMemoryError, and I have increased the initial heap size to 2G for Eclipse: -Xms2g -Xmx4g -Xss2m (which get passed to JVM). But that did not solve the problem.

Then I thought maybe I should garbage collect the List<Integer> edge variable, by calling System.gc(), in case its memory does not get cleared. That did not work either.

I was thinking maybe the problem is with the List<List<Integer>> data structure. I tried List<int[]>, which lasted a bit longer: more edges are added before OutOfMemoryError happens. I do not have a better idea right now.

I have searched around for similar problems, but have not find much help. I wonder if anyone has experience with this kind of situation.

like image 594
Ethan Avatar asked Dec 29 '25 15:12

Ethan


1 Answers

To let your program use more memory from Eclipse:

Go to Run -> Run Configurations. You will see this window Run Configurations

Click on Arguments Run Configurations/Arguments

Enter your arguments to the VM Run Configurations/Arguments/VM Arguments

like image 101
Jeffrey Avatar answered Jan 01 '26 03:01

Jeffrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!