java.lang.OutOfMemoryError: Java heap space

前端 未结 11 1065
温柔的废话
温柔的废话 2020-11-22 08:01

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

java.lang.OutOfMemoryError: Java heap space

The above error occu

相关标签:
11条回答
  • 2020-11-22 08:51

    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

    0 讨论(0)
  • 2020-11-22 08:52
    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.

    0 讨论(0)
  • 2020-11-22 08:53

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

    -Xmx256M
    
    0 讨论(0)
  • 2020-11-22 08:55
    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

    0 讨论(0)
  • 2020-11-22 08:58

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

    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
    
    0 讨论(0)
提交回复
热议问题