Jenkins

How to Fix the Jenkins OutOfMemoryError

One of the most prominent errors when working with Jenkins is the OutOfMemory error. This tutorial discusses why this error might appear in your Jenkins and how to fix it.

What Is the OutOfMemoryError?

An “OutOfMemoryError” in Java is an error that occurs when the Java Virtual Machine (JVM) does not have enough memory available to execute a given operation successfully.

It can be caused by various reasons such as:

  1. The JVM has been configured with a small max heap size. Therefore, when an application that runs on the JVM needs more heap size than the set max size, it fails, leading to the OutOfMemoryError.
  2. The application has a potential memory leak, causing it to retain more and more memory over time.

In order to resolve an OutOfMemoryError, we need to identify the cause of the error and take an appropriate action.

However, this is too broad and can apply to any application that runs on the JVM. Let us narrow down what can cause an OutOfMemoryError in Jenkins.

Causes of OutOfMemoryError in Jenkins

In the context of the Jenkins cluster, there are four main causes of this error. They include the following:

  1. Increasing the Jenkins data size. As the size of your data in your Jenkins instance increases, the application also requires a bigger heap size.
  2. Jenkins might be temporarily processing a large amount of data.
  3. Potential memory leak in Jenkins.
  4. The host system kernel is running out of virtual memory.

Diagnosing the Main Cause of the OutofMemoryError in Jenkins

We established four potential causes of this type of error in Jenkins. How do we narrow down the main cause of this error?

In such a case, we can use a tool such as VisualVM which allows us to visualize the memory usage of the JVM on our system.

This tool can pinpoint the main cause of the OutOfMemoryError when working with Jenkins.

Navigate to the following link and download your machine’s latest version of the VisualVM tool.

https://visualvm.github.io/

Once downloaded, run the app and select “Local” to view the JVM usage on your local machine.

This should show you the details of your system including the available physical memory, swap memory, and more.

To determine the cause of this error, stop Jenkins and start it again while monitoring the JVM memory usage on your local machine.

If the memory usage maxes out while starting Jenkins, the cause is a small heap size.

However, if the memory usage slowly increases, this may be a potential memory leak on your Jenkins version.

If Jenkins uses all the available virtual memory, t the host kernel is running out of virtual memory. You can fix this by increasing the available virtual memory or terminating the running Jenkins builds.

How to Configure the Heap Size for the JVM

If you are facing the OutOfMemoryError due to a small heap size, the solution is just to allocate a larger heap size.

We can configure the JVM’s initial and maximum heap size using the -Xms and -Xmx parameters.

NOTE: It is recommended to set the same value for both -Xms and Xmx for the JVM.

Although the sufficient heap size may vary depending on the available system resources and Jenkins workload, it is recommended to set the heap size to 2GB (minimum) and 16GB (maximum).

You can increase the heap size for Jenkins on Linux. You can edit the Jenkins startup service with the following command:

$ sudo systemctl edit jenkins

You can also edit the service file manually, as shown in the following:

sudo vim /usr/lib/systemd/system/jenkins.service

Locate the JAVA_OPTS option and add the -Xms and -Xmx options as shown in the following:

Environment="JAVA_OPTS=-Djava.awt.headless=true -Xms1G -Xmx1G"

Adding the previous options increase the heap memory size to 1GB. Once completed, you can restart the Jenkins service as follows:

$ sudo systemctl restart jenkins

You can increase the -Xms and Xmx values for Jenkins on Windows by editing the configuration file.

By default, the configuration file is located in C:\Program Files\Jenkins\jenkins.xml.

Edit the arguments block of the XML file as shown in the following:

Once edited, save the file and restart the Jenkins service.

Conclusion

You discovered the cause of the OutOfMemoryError when working with Jenkins. You also learned how to troubleshoot the various causes of this error. Finally, you now learned the multiple methods of fixing it.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list