How to fix Terminating due to java.lang.OutOfMemoryError: Java heap space error

Dhal, Sushreesmita 0 Reputation points
2024-01-30T13:31:45.9966667+00:00

My application is restarting multiple times with Terminating due to java.lang.OutOfMemoryError: Java heap space error. my configuration is like below , Please help on this

- name: JVM_OPTS
            value: -Xms5G -Xmx5G -XX:MaxMetaspaceSize=1024m -XX:ParallelGCThreads=16 -XX:+ExitOnOutOfMemoryError -javaagent:agent/applicationinsights-agent-3.4.12.jar   
Azure Container Instances
Azure Container Instances
An Azure service that provides customers with a serverless container experience.
757 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,416 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2024-01-30T13:31:46+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To fix the java.lang.OutOfMemoryError: Java heap space error, you can increase the maximum heap size allocated to the JVM. In your current configuration, you have set the initial heap size and maximum heap size to 5GB using the -Xms and -Xmx JVM options. However, this may not be sufficient for your application's memory requirements.

    You can try increasing the maximum heap size to a higher value, such as 8GB or 10GB, and see if it resolves the issue. You can do this by modifying the -Xmx option in your configuration.

    - name: JVM_OPTS
      value: -Xms5G -Xmx10G -XX:MaxMetaspaceSize=1024m -XX:ParallelGCThreads=16 -XX:+ExitOnOutOfMemoryError -javaagent:agent/applicationinsights-agent-3.4.12.jar
    

    Keep in mind that setting the maximum heap size too high can also cause issues such as container OOM, so you need to find a balance between the memory requirements of your application and the available resources.


    References:

    0 comments No comments

  2. sree pathy 0 Reputation points
    2025-06-12T11:03:25.7766667+00:00

    I see your application tends to restart multiple times due to the error it faces, java.lang.OutOfMemoryError: Java heap space. This usually happens when the allocated JVM heap memory is not sufficient to handle any executions based on the application’s memory requirements.

    In order to address this, the basic thing you will have to do is to increase the heap size if your infrastructure allows it. For example, increasing both -Xms and -Xmx to 6GB or higher might help stabilize the application. Along with this, I will suggest that it's a good idea to enable heap dump generation on OutOfMemoryError. By doing so, you will be generated with a memory dump file whenever an issue occurs. Now you can analyze using tools like Eclipse MAT or VisualVM to identify what’s consuming so much memory.

    Apart from this, you should try to enable GC (Garbage Collection) logs. The logs generated will help you to monitor how memory is being allocated and released. This will give you an idea of how GC is happening, its frequency and also will point out to us if any unnecessary objects are present.  

    In general check for memory leaks, large objects being retained in memory, or inefficient use of collections and caches. Once you start to optimize these areas you can find a significant reduction of memory pressure.

    Suppose, if your application is currently using the default or Parallel GC, consider switching to G1GC. This is actually better suited for handling larger heaps and providing more predictable pause times.

    At last,  I would say the setting -XX:ParallelGCThreads=16 might be unnecessarily high for a 5GB heap unless you're running on a high-core machine. Try to tune this value to match your CPU resource. This will also have an improved performance.

    After making these adjustments, monitor your application’s memory behavior and GC logs again to verify the improvements.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.