java.lang.OutOfMemoryError: Java heap space
Asked Answered
T

12

110

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?

Timer answered 20/10, 2009 at 17:2 Comment(1)
Check #37835Biliary
A
111

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.

Amaral answered 20/10, 2009 at 17:8 Comment(4)
I 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.Timer
I 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.Amaral
Default values change with java versions, it would be nice to include this information in your answer.Grum
Just 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.Solo
L
30

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

Leftist answered 20/10, 2009 at 17:9 Comment(0)
P
8

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 (internet-archive link)

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.

Padauk answered 20/10, 2009 at 17:9 Comment(0)
M
7

To increase the heap size you can use the -Xmx argument when starting Java; e.g.

-Xmx256M
Mucker answered 20/10, 2009 at 17:7 Comment(0)
C
7

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

Climatology answered 18/12, 2013 at 11:4 Comment(0)
E
6
  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.

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)

  1. 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

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

It depends on your application.

  1. Set the maximum heap memory as per your application requirement

  2. Don't cause memory leaks in your application

  3. 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:

  1. Improper configuration ( not allocating sufficiant memory)
  2. Application is unintentionally holding references to objects and this prevents the objects from being garbage collected
  3. 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

Effusion answered 15/2, 2016 at 12:2 Comment(0)
C
5
  1. In most of the cases, the code is not optimized. Release those objects which you think shall not be needed further. Avoid creation of objects in your loop each time. Try to use caches. I don't know how your application is doing. But In programming, one rule of normal life applies as well

    Prevention is better than cure. "Don't create unnecessary objects"

Cilurzo answered 22/10, 2009 at 17:28 Comment(0)
L
3
  1. Local variables are located on the stack. Heap space is occupied by objects.

  2. You can use the -Xmx option.

  3. Basically heap space is used up everytime you allocate a new object with new and freed some time after the object is no longer referenced. So make sure that you don't keep references to objects that you no longer need.

Landsman answered 20/10, 2009 at 17:9 Comment(0)
M
1

No, I think you are thinking of stack space. Heap space is occupied by objects. The way to increase it is -Xmx256m, replacing the 256 with the amount you need on the command line.

Malady answered 20/10, 2009 at 17:7 Comment(0)
C
1

To avoid that exception, if you are using JUnit and Spring try adding this in every test class:

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
Chichi answered 15/1, 2019 at 9:0 Comment(0)
I
1

I have tried all Solutions but nothing worked from above solutions

Solution: In My case I was using 4GB RAM and due to that RAM usage comes out 98% so the required amount if Memory wasn't available. Please do look for this also.If such issue comes upgrade RAM and it will work fine.

Hope this will save someone Time

Ilion answered 20/5, 2021 at 12:0 Comment(5)
If 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?Fealty
I 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 FineIlion
But to be honest, 12 GB RAM is only required when your audience is more than a million. How is your user base?Fealty
@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. THanksIlion
I 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.Rumor
C
0

In netbeans, Go to 'Run' toolbar, --> 'Set Project Configuration' --> 'Customise' --> 'run' of its popped up windo --> 'VM Option' --> fill in '-Xms2048m -Xmx2048m'. It could solve heap size problem.

Cohdwell answered 12/6, 2017 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.