Francesco Marra
Francesco Marra

Reputation: 31

Memory overflow Java

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.

Upvotes: 0

Views: 226

Answers (4)

Sameer Patel
Sameer Patel

Reputation: 19

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.

Upvotes: 1

Charlie
Charlie

Reputation: 986

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

Upvotes: 0

Maksym
Maksym

Reputation: 4584

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.

Upvotes: 1

DanielGibbs
DanielGibbs

Reputation: 10190

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.

Upvotes: 1

Related Questions