1

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

Using a 2D array of [100000][100000] and other two arrays of [100000] each. I need these three arrays in the whole program so can't free their memory.

Already tried VM Options -Xmx512m in Netbeans

Please be specific and Step by Step, I am newbie in Java and Netbeans.

Thanks in advance for you help....

7
  • 1
    What the type of the array, try run with -Xmx1200m (max of window xp) or more almost up to your maximum free memory Commented Mar 14, 2015 at 22:58
  • That 2D array is going to take 10GB if the elements are bytes, 40GB if they're ints... So you're going to need at least element_size_in_bytes * 10 GB of RAM... Commented Mar 14, 2015 at 23:02
  • You just need to apply what you learnt in arithmetics: 100000 * 100000 = 10 billion. Asuming these are arrays of bytes, that means you need 10 billion bytes, i.e. 10 GB of memory. Your 512MB won't cut it. Commented Mar 14, 2015 at 23:05
  • 2
    @RoyShmuli That's not guaranteed, booleans can be bigger than 1 bit. Commented Mar 14, 2015 at 23:08
  • 1
    @RoyShmuli, although VM dependent, Java's boolean take a about a bute of memory in practice: stackoverflow.com/questions/383551/… Commented Mar 14, 2015 at 23:23

1 Answer 1

6

Let's do some math. You're allocating a 10,000,000,000 element two dimensional array, plus another two arrays of 100,000 elements.

That's 10,002,000,000 elements. If each of them is an int, that's 40,008,000,000 bytes. That's 37.26 Giga bytes.

Your -Xmx512m isn't nearly enough, you need something closer to -Xmx60G if these are really ints or -Xmx15G in the best case scenario, in which the elements are bytes (e.g. booleans). But that will probably won't work since you (probably) don't have enough physical memory. To me it sounds like you need some disk backed storage, or a database.

Either re-think what you're doing and how you're doing it, or use a machine with that much physical memory.

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

4 Comments

Thanks Bart... Got your point... Didn't thought in that way... how to work around this? I am working on graphs with around 80K vertices and then finding a shortest path between two given vertices.
@ManojPathak If you can't fit the entire graph in memory, store it on the disk, and read only relevant pieces of data each time. Something like Neo4j (en.wikipedia.org/wiki/Neo4j) might be useful.
Thanks for your suggestions they are really helpful :-)
You have probably chosen wrong algorithm. There is a lot of algorithms, that doesn't require NxN matrix. Consider Dijkstra's algorithm if you have a graph with positive costs or Bellman-Ford's algorithm if you don't or A* if you have a good heuristic. Those algorithms are fast and don't require much space. I've been working at graphs with 100 000's of vertices and millions of edges on high-end desktop computer and it worked well.

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.