How do I give Jenkins more heap space when it's running as a daemon on Ubuntu?

Solution 1:

There are two types of OutOfMemoryError messages that you might encounter while a Jenkins job runs:

  • java.lang.OutOfMemoryError: Heap space – this means that you need to increase the amount of heap space allocated to Jenkins when the daemon starts.
  • java.lang.OutOfMemoryError: PermGen space – this means you need to increase the amount of generation space allocated to store Java object metadata. Increasing the value of the -Xmx parameter will have no affect on this error.

On Ubuntu 12.04 LTS, uncomment the JAVA_ARGS setting on line ten of /etc/default/jenkins:

  • To add more Java heap space, increase the value of the -Xmx Java parameter. That sets the maximum size of the memory allocation pool (the garbage collected heap).
  • To add more PermGen space, add the parameter XX:MaxPermSize=512m (replace 512 with something else if you want more. The permanent generation heap holds meta information about user classes.

For example, this extract is from the default /etc/default/jenkins after a fresh install of Jenkins:

# arguments to pass to java
#JAVA_ARGS="-Xmx256m"

This is how it would look if you set the heap space to be 1 GB:

# arguments to pass to java
JAVA_ARGS="-Xmx1048m"

Be careful not to set the heap size too large, as whatever you allocate reduces the amount of memory available to the operating system and other programs, which could cause excessive paging (memory swapped back and forth between RAM and the swap disc, which will slow your system down).

If you also set MaxPermSpace, you need to add a space between the parameters):

# arguments to pass to java
JAVA_ARGS="-Xmx1048m -XX:MaxPermSize=512m"

After making a change, restart Jenkins gracefully from the Jenkins web interface, or force an immediate restart from the command-line with sudo /etc/init.d/jenkins restart.

I found the following site useful for understanding Java maximum and permanent generation heap sizes: http://www.freshblurbs.com/blog/2005/05/19/explaining-java-lang-outofmemoryerror-permgen-space.html.

Solution 2:

For CentOS, the directory the Jenkins.xml is located in by default is /etc/sysconfig/ for jenkins-1.579-1.1

JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Xmx -XX:MaxPermSize="

Solution 3:

If you are using Ubuntu Server, first install Monitoring plugin to see how much memory Jenkins is using. For example, this is what I saw after installing it:

enter image description here

Then, with the command free -m, I figured out what was the server's memory size. In my case, 16Gb. With that info, I opened /etc/default/jenkins and changed:

JAVA_ARGS="-Djava.awt.headless=true"

to

JAVA_ARGS="-Xmx8384m -Djava.awt.headless=true"

Where 8384 is 8Gb. Then I restarted Jenkins with the command sudo service jenkins restart and then, after triggering the job that was getting memory issues, things looked much better and the job could complete on this and subsequent runs:

enter image description here

Solution 4:

Another method to set the heap size for particular jobs is to use environment variables for each job. This ensures that the memory is available when the job that requires a higher memory is not in use.

GRADLE_OPTS="-Dorg.gradle.jvmargs=-Xms1024M -Xmx8192M -XX:PermSize=512M -XX:MaxPermSize=2048 -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

JAVA_OPTS="-XX:MaxPermSize=2048M"

Jenkins job config