Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.OutOfMemoryError: Java heap space

I am getting the following error on execution of a multi-threading program

java.lang.OutOfMemoryError: Java heap space 

The above error occured in one of the threads.

  1. Upto my knowledge, Heap space is occupied by instance variables only. If this is correct, then why this error occurred after running fine for sometime as space for instance variables are alloted at the time of object creation.

  2. Is there any way to increase the heap space?

  3. What changes should I made to my program so that It will grab less heap space?

like image 210
Yatendra Avatar asked Oct 20 '09 17:10

Yatendra


2 Answers

If you want to increase your heap space, you can use java -Xms<initial heap size> -Xmx<maximum heap size> on the command line. By default, the values are based on the JRE version and system configuration. You can find out more about the VM options on the Java website.

However, I would recommend profiling your application to find out why your heap size is being eaten. NetBeans has a very good profiler included with it. I believe it uses the jvisualvm under the hood. With a profiler, you can try to find where many objects are being created, when objects get garbage collected, and more.

like image 97
Thomas Owens Avatar answered Oct 18 '22 13:10

Thomas Owens


1.- Yes, but it pretty much refers to the whole memory used by your program.

2.- Yes see Java VM options

-Xms<size>        set initial Java heap size -Xmx<size>        set maximum Java heap size 

Ie

java -Xmx2g assign 2 gigabytes of ram as maximum to your app

But you should see if you don't have a memory leak first.

3.- It depends on the program. Try spot memory leaks. This question would be to hard to answer. Lately you can profile using JConsole to try to find out where your memory is going to

like image 37
OscarRyz Avatar answered Oct 18 '22 14:10

OscarRyz