Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


1. Overview

Simply put, the JVM takes care of freeing up memory when objects are no longer being used. This process is called Garbage Collection (GC).

The GC Overhead Limit Exceeded error is one from the java.lang.OutOfMemoryError family, and it's an indication of a resource (memory) exhaustion.

In this quick tutorial, we'll look at what causes the java.lang.OutOfMemoryError: GC Overhead Limit Exceeded error and how it can be solved.

2. GC Overhead Limit Exceeded Error

OutOfMemoryError is a subclass of java.lang.VirtualMachineError. It's thrown by the JVM when it encounters a problem related to utilizing resources. More specifically, the error occurs when the JVM spent too much time performing Garbage Collection and was only able to reclaim very little heap space.

According to Java docs, by default, the JVM is configured to throw this error if the Java process spends more than 98% of its time doing GC and when only less than 2% of the heap is recovered in each run. In other words, this means that our application has exhausted nearly all the available memory, and the Garbage Collector has spent too much time trying to clean it and failed repeatedly.

In this situation, users experience extreme slowness of the application. Certain operations, which usually complete in milliseconds, take more time to complete. This is because the CPU is using its entire capacity for Garbage Collection and therefore cannot perform any other tasks.

3. Error in Action

Let's look at a piece of code that throws java.lang.OutOfMemoryError: GC Overhead Limit Exceeded.

We can achieve that, for example, by adding key-value pairs in an unterminated loop:

public class OutOfMemoryGCLimitExceed {
    public static void addRandomDataToMap() {
        Map<Integer, String> dataMap = new HashMap<>();
        Random r = new Random();
        while (true) {
            dataMap.put(r.nextInt(), String.valueOf(r.nextInt()));

When this method is invoked, with the JVM arguments as -Xmx100m -XX:+UseParallelGC (Java heap size is set to 100 MB and the GC Algorithm is ParallelGC), we get a java.lang.OutOfMemoryError: GC Overhead Limit Exceeded error. To get a better understanding of different Garbage Collection Algorithms, check out Oracle's Java Garbage Collection Basics tutorial.

We'll get a java.lang.OutOfMemoryError: GC Overhead Limit Exceeded error very quickly by running the following command from the root of the project:

mvn exec:exec

It should also be noted that in some situations we might encounter a heap space error before encountering the GC Overhead Limit Exceeded error.

4. Solving GC Overhead Limit Exceeded Error

The ideal solution is to find the underlying problem with the application by examining the code for any memory leaks.

These questions need to be addressed:

  • What are the objects in the application that occupy large portions of the heap?
  • In which parts of the source code are these objects being allocated?

We can also use automated graphical tools such as JConsole, which helps to detect performance problems in the code including java.lang.OutOfMemoryErrors.

The last resort would be to increase the heap size by altering the JVM launch configuration.

For example, this gives 1 GB heap space for the Java application:

java -Xmx1024m com.xyz.TheClassName

However, this will not solve the problem if there are memory leaks in the actual application code. Instead, we will just postpone the error. So, it is more advisable to thoroughly reassess the memory usage of the application.

5. Conclusion

In this article, we examined the java.lang.OutOfMemoryError: GC Overhead Limit Exceeded and the reasons behind it.

As always, the source code related to this article can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


Generic footer banner
Comments are closed on this article!