0

I think that my Java Program exceeded the usage of memory allowed... this is the error that shows up in Eclipse:

 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Main.main(Main.java:228)

before than trying an implementation with better memory handling, is there any way basically to augment the memory to allocate for the program ? I would like to see the program terminating and showing the result at least.

  • 1
    possible duplicate of [Increase heap size in java](http://stackoverflow.com/questions/1565388/increase-heap-size-in-java) – dimo414 Nov 16 '14 at 14:25

4 Answers4

1

You can increase heap size using the cmd-line flags

For example:

java -Xmx6g myprogram

You can get a full list (or a nearly full list, anyway) by typing java -X.

Rishi Dua
  • 2,296
  • 2
  • 24
  • 35
1

You can allocate more memory to the process by using the -Xmx flag:

java -Xmx2G YourProgram

Will allocate 2 Gigabytes of memory to the process.

You can do this in Eclipse by going to Run Configurations, the Arguments section and adding -Xmx 2G to the VM arguments.

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
1

Increasing memory size is good, but you should also consider that probably your program has some memory leak and you really should bother about it.

Maksym
  • 4,434
  • 4
  • 27
  • 46
0

As I don't think your program is using that much memory, I suspect your program is having a memory leak somewhere.
If you could give Main.java:200 till Main.java:250 we could check for any leaks.

If you are sure your program is using that amount of memory you can either run
java -Xmx2G -jar foo.jar in the Command Prompt (Windows) or Terminal (Mac OS X or Linux)
or, if you're running your program in Eclipse under Linux, do the following:

1) Make sure Eclipse is closed
2) Open your favorite text editor at your eclipse.ini file (Default location: /usr/lib/eclipse/eclipse.ini)
3) Search for --Xmx512M and increase this number (i.e. --Xmx2G)
4) Save the file and restart Eclipse

For any explaination about --Xmx and --Xms I refer to Eclipse memory settings when getting "Java Heap Space" and "Out of Memory"

Happy coding :) -Charlie

Community
  • 1
  • 1
Charlie
  • 978
  • 1
  • 7
  • 27
  • Thank your for your answer. I am running a Ford Fulkerson algorithm to find a max flow in a directed graph. The graph is represented as a matrix adjecency .. and I suspect that its size can be like 17 000 * 17 000 )) – Francesco Marra Nov 16 '14 at 15:04
  • @FrancescoMarra If you're using a large algorithm (like you do) you can expect memory issues. Follow the steps I (and others) have written down to increase the `Java Heap Space`. – Charlie Nov 16 '14 at 15:07