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

4 Answers 4

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.

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

Comments

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.

Comments

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.

Comments

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

2 Comments

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 ))
@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.

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.