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:
- 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.
- 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:
- Increasing the Jenkins data size. As the size of your data in your Jenkins instance increases, the application also requires a bigger heap size.
- Jenkins might be temporarily processing a large amount of data.
- Potential memory leak in Jenkins.
- 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.
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:
You can also edit the service file manually, as shown in the following:
Locate the JAVA_OPTS option and add the -Xms and -Xmx options as shown in the following:
Adding the previous options increase the heap memory size to 1GB. Once completed, you can restart the Jenkins service as follows:
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.