Home » Core Java » GC Overhead Limit Exceeded

About Yatin

Avatar photo
An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).

GC Overhead Limit Exceeded

Hello. In this tutorial, we will understand the GC Overhead Limit Exceeded error in the Java programming language.

1. Introduction

Out of memory error in java is a virtual machine error thrown by the java virtual machine when the resources are exhausted. In other words, the error prevails when the virtual machine has spent hours performing the garbage collection but has reclaimed very little heap space. This error is thrown when the JVM spends 98% of the time performing garbage collection but only 2% of the heap space is recovered in each run.

2. Practice

Let us dive into some practice stuff from there and I am assuming that you already have the Java 1.8 or greater installed on your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Setting up the pom

Create a java project and dd the following code to the pom file. Since this is a simple java application so we will not use any fancy dependencies and will simply set the java version property.

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <artifactId>garbagecollectionerror</artifactId>
  <groupId>com.learning</groupId>
  <modelVersion>4.0.0</modelVersion>
  <name>garbage-collection-error</name>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <version>0.0.1-SNAPSHOT</version>
</project>

2.2 Creating the implementation class

Add the following code to the java class created in the com.learning package under the src/main/java folder. The code will keep adding the random names to a list in an unterminated loop.

GCError.java

package com.learning;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GCError {

	static int count = 0;
	static Random random = new Random();
	
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		while (true) {
			names.add("Random name" + random.nextInt());
			System.out.println(count++);
		}
	}
}

Run the file as a java file with the following command – java -Xmx100m -XX:+UseParallelGC GCError.java. The logic will work infinitely to add the random name to the ArrayList and print the count on the console. Once the memory gets exhausted the below error will be thrown on the IDE console.

Console output

-- long output snipped for brevity …

79934
79935
79936
79937
79938
79939
79940
...

java.lang.OutOfMemoryError: GC Overhead Limit Exceeded error

2.3 Solving error

To solve the garbage collection error in a java program one must ensure that are fewer or minimum leaks in the system by –

  • Identify the large objects in the code and mark them as null once the execution is done or they are not needed anymore
  • Avoid creating temporary or weakly referenced objects in the java application code
  • Increase the heap size for the java application to avoid the out-of-memory error e.g. java -Xmx1024m com.learning.GCError.java

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we discussed the garbage collection in java and how to solve it. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial on GC overhead limit exceeded error in java programming.

Download
You can download the full source code of this example here: GC Overhead Limit Exceeded

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments