3

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.

4
  • What about creating a class edge with only two int field? Commented Feb 16, 2015 at 1:24
  • 3
    No the parameters you set for eclipse does not get passed to the program inside Commented Feb 16, 2015 at 1:24
  • Check the Eclipse launch configuration, Arguments tab, VM arguments text box; use an appropriate -Xmx argument there. Probably your jvm is being launched with the default limit, which is often 256MB. Also please look what's going on in your post with less-than and greater-than characters because it sure doesn't look right. Commented Feb 16, 2015 at 1:29
  • @chrislott, thank you for your comment about the VM argument in run config. I think by "less-than and greater-than characters" you are referring to the angle brackets for the generics? Commented Feb 16, 2015 at 2:47

2 Answers 2

5

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

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

Comments

0

Since you are using a lot of RAM besides setting the max heap parameter, make sure you use 64-bit Java. The 32-bit is limited to 2 Gigs or something like that.

Also, for large graphs you should consider using a database.

And last but not least, maybe you can rethink your algorithm, sometimes you just don't need all the nodes and edges.

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.