java.lang.OutOfMemoryError: Java heap space
Solution 1
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.
Solution 2
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
Solution 3
You may want to look at this site to learn more about memory in the JVM: http://developer.streamezzo.com/content/learn/articles/optimization-heap-memory-usage
I have found it useful to use visualgc to watch how the different parts of the memory model is filling up, to determine what to change.
It is difficult to determine which part of memory was filled up, hence visualgc, as you may want to just change the part that is having a problem, rather than just say,
Fine! I will give 1G of RAM to the JVM.
Try to be more precise about what you are doing, in the long run you will probably find the program better for it.
To determine where the memory leak may be you can use unit tests for that, by testing what was the memory before the test, and after, and if there is too big a change then you may want to examine it, but, you need to do the check while your test is still running.
Solution 4
You can get your heap memory size through below programe.
public class GetHeapSize {
public static void main(String[] args) {
long heapsize = Runtime.getRuntime().totalMemory();
System.out.println("heapsize is :: " + heapsize);
}
}
then accordingly you can increase heap size also by using: java -Xmx2g http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
Solution 5
- 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.
That means you are creating more objects in your application over a period of time continuously. New objects will be stored in heap memory and that's the reason for growth in heap memory.
Heap not only contains instance variables. It will store all non-primitive data types ( Objects). These objects life time may be short (method block) or long (till the object is referenced in your application)
- Is there any way to increase the heap space?
Yes. Have a look at this oracle article for more details.
There are two parameters for setting the heap size:
-Xms:, which sets the initial and minimum heap size
-Xmx:, which sets the maximum heap size
- What changes should I made to my program so that It will grab less heap space?
It depends on your application.
Set the maximum heap memory as per your application requirement
Don't cause memory leaks in your application
If you find memory leaks in your application, find the root cause with help of profiling tools like MAT, Visual VM , jconsole etc. Once you find the root cause, fix the leaks.
Important notes from oracle article
Cause: The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak.
Possible reasons:
- Improper configuration ( not allocating sufficiant memory)
- Application is unintentionally holding references to objects and this prevents the objects from being garbage collected
- Applications that make excessive use of finalizers. If a class has a finalize method, then objects of that type do not have their space reclaimed at garbage collection time. If the finalizer thread cannot keep up, with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError exception would be thrown.
On a different note, use better Garbage collection algorithms ( CMS or G1GC)
Have a look at this question for understanding G1GC
Related videos on Youtube
Amit
Updated on May 21, 2021Comments
-
Amit almost 3 years
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.
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.
Is there any way to increase the heap space?
What changes should I made to my program so that It will grab less heap space?
-
JuanZe over 14 years
-
Amit over 14 yearsI am using Netbeans but I don't know how to use profiler. I would like to know more about the profiler so that I can use it to find memory leaks in my application.
-
Thomas Owens over 14 yearsI added a link to a page on the NetBeans site (profiler.netbeans.org) which has very good documentation about the profile, from the very basics to more advanced use.
-
Dariusz about 8 yearsDefault values change with java versions, it would be nice to include this information in your answer.
-
hipokito almost 8 yearsJust fixed similar problem and first tried: java -jar division.jar -Xmx512m -Xms512m - this gives me same error but when I do it like so: java -Xmx512m -Xms512m -jar division.jar - all is fine. So the order of params is also important.
-
Asu about 7 years@hipokito Arguments after the jar file are passed on to the jar file's main() method as args[]
-
Kishan Solanki almost 3 yearsIf 4 GB RAM was used then How much RAM you're having right now? After increasing the RAM, are you having any problems with memory errors?
-
RvSingh3213 almost 3 yearsI have Added 8GB more and Now 12Gb is working Fine. No after Increasing RAM I dont face
java.lang.OutOfMemoryError: Java heap space
this Issue and Now it is working Perfectly Fine -
Kishan Solanki almost 3 yearsBut to be honest, 12 GB RAM is only required when your audience is more than a million. How is your user base?
-
RvSingh3213 almost 3 years@KishanSolanki Actually I got this Error in Local Machine. So I have waster too much time searching for Solution. So I Have posted this Answer so that It can help someone who is getting Local Issue. I think Solution mentioned by me will work if you face this Issue in Local. THanks
-
arunken over 2 yearsI doubt that increasing the RAM is a solution as you are running the application locally without much load. There is a high chance of of memory leak in your application or you have configured some JVM argument which is causing the issue under 8GB system RAM.