A logo showing the text blog.marcnuri.com
Español
Home»Java»java.lang.OutOfMemoryError: GC overhead limit exceeded

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

java.lang.OutOfMemoryError: GC overhead limit exceeded

2014-12-20 in Java tagged Java / JVM / Memory / Performance by Marc Nuri | Last updated: 2025-01-19
Versión en Español

Introduction

The java.lang.OutOfMemoryError: GC overhead limit exceeded error is a common issue that Java developers encounter when their applications consume too much CPU time on garbage collection with little memory reclaimed. The Java Virtual Machine (JVM) triggers this error when it spends over 98% of the total execution time on garbage collection but reclaims less than 2% of the heap memory during that period.

Understanding how the JVM handles garbage collection and memory allocation is crucial for maintaining high-performing Java applications.

In this article, I'll explore the causes of the java.lang.OutOfMemoryError: GC overhead limit exceeded error, how to diagnose and fix it, and best practices to avoid it in the future.

What causes the OutOfMemoryError: GC overhead limit exceeded error?

The java.lang.OutOfMemoryError: GC overhead limit exceeded error occurs when the JVM spends too much time performing garbage collection and recovers too little memory.

This error is typically caused by one of the following reasons:

  1. Memory Leak: Your application may have a memory leak, causing it to consume more memory than necessary.
  2. Inefficient Code: Your application may be inefficiently using memory, leading to excessive garbage collection.
  3. Large Heap Size: If your application has a large heap size, garbage collection may take longer to complete.
  4. Inadequate Heap Size: If your application has an inadequate heap size, garbage collection may not be able to reclaim enough memory.

The following code snippet shows an example of a memory leak that can lead to the java.lang.OutOfMemoryError: GC overhead limit exceeded error:

MemoryLeak.java
import java.util.ArrayList;
import java.util.List;

public class MemoryLeak {

  // List to store Long objects
  private static final List<Long> longs = new ArrayList<>();

  public static void main(String[] args) {
    // Infinite loop to add Long.MAX_VALUE to the list
    while (true) {
      longs.add(Long.MAX_VALUE);
    }
  }
}

In this example, Long.MAX_VALUE is repeatedly added to the list, simulating unbounded memory growth. The list's persistent reference prevents garbage collection from reclaiming memory.

If we execute the MemoryLeak class with the following command:

bash
java -Xmx10m -XX:+UseParallelGC MemoryLeak.java

The Xmx flag sets the maximum heap size to 10MB, and the UseParallelGC flag specifies the Parallel Garbage Collector. Since the MemoryLeak class adds Long.MAX_VALUE to the longs list in an infinite loop, the list will grow indefinitely. The garbage collector will try to reclaim memory, but since the list is still growing and referenced by the main class, it will spend most of its time doing garbage collection with little memory reclaimed.

The JVM will eventually (and quickly) run out of memory and throw the java.lang.OutOfMemoryError: GC overhead limit exceeded error:

bash
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
        at java.base/java.lang.Long.valueOf(Long.java:1204)
        at MemoryLeak.main(MemoryLeak.java:9)
        at java.base/java.lang.invoke.LambdaForm$DMH/0x00007fab38030000.invokeStatic(LambdaForm$DMH)
        at java.base/java.lang.invoke.LambdaForm$MH/0x00007fab3813d400.invoke(LambdaForm$MH)
        at java.base/java.lang.invoke.Invokers$Holder.invokeExact_MT(Invokers$Holder)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invokeImpl(DirectMethodHandleAccessor.java:154)

How to diagnose and fix the OutOfMemoryError: GC overhead limit exceeded error?

To diagnose and fix the java.lang.OutOfMemoryError: GC overhead limit exceeded error, follow these steps:

  1. Analyze Heap Dumps: Use a heap dump analysis tool like VisualVM, JProfiler, or YourKit to identify memory leaks and inefficient memory usage.
  2. Optimize Code: Identify and fix memory leaks, such as unintentional object retention in collections, and ensure efficient use of memory-intensive operations like caching or object creation.
  3. Tune Garbage Collection:
    • Adjust the garbage collection settings to optimize memory usage and reduce garbage collection overhead.
    • Consider using a different garbage collector, such as the G1 Garbage Collector (-XX:+UseG1GC), which is designed for large heaps and low-latency applications.
  4. Increase Heap Size: If your application is running out of memory, consider increasing the heap size to allow more memory for garbage collection.

Your process should make sure that the root cause of the OutOfMemoryError is not a memory leak or inefficient memory usage. Once and only when you've ensured the heap usage is optimal and genuine, you can adjust the garbage collection settings or increase the heap size to avoid the error.

Conclusion

The java.lang.OutOfMemoryError: GC overhead limit exceeded error is a common issue in Java applications that consume too much CPU time on garbage collection. Diagnosing and fixing this error involves identifying the root cause through heap dump analysis, resolving memory inefficiencies, and tuning JVM settings to optimize garbage collection and memory allocation. By following these best practices, you can avoid the OutOfMemoryError error and improve the performance and stability of your Java applications.

References

  • Stack Overflow: Error java.lang.OutOfMemoryError: GC overhead limit exceeded
  • Oracle: HotSpot Virtual Machine Garbage Collection Tuning Guide
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Running Apache Tomcat and Apache HTTPD on port 80 simultaneouslyWhat is a Java Heap dump?
© 2007 - 2025 Marc Nuri