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

97,696

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

Share:
97,696
Steve HHH
Author by

Steve HHH

Updated on July 09, 2022

Comments

  • Steve HHH
    Steve HHH almost 2 years

    My Jenkins jobs are running out of memory, giving java.lang.OutOfMemoryError messages in the build log. But I used the Ubuntu Package Manager, aptitude, or apt-get to install Jenkins, and I don't know where to look to change the amount of heap space allocated to Jenkins.

  • Alper Akture
    Alper Akture about 11 years
    I think you forgot the seconds dash, so it should be: JAVA_ARGS="-Xmx1048m -XX:MaxPermSize=512m"
  • Steve HHH
    Steve HHH about 11 years
    Thank-you! I've added the missing dash.
  • CasualT
    CasualT over 10 years
    I found that on my Ubuntu install that I had to go into /etc/init/jenkins.conf and add to the line JAVA_OPTS="". (editing the /etc/default location had no effect, and I confirmed this with jmap).
  • FilBot3
    FilBot3 over 9 years
    Is there way to do this in CentOS? I'm not finding a /etc/default/jenkins directory. And all fo the files that say Jenkins, or are located in jenkins directories don't say anything about Xmx, or memory.
  • bmaher
    bmaher about 9 years
    Cheers, I was nearly going mad trying to find this
  • user1164061
    user1164061 almost 9 years
    @Pred did you find a solution to your problem? I have the same issue with my jenkins slave and I dont know where I should these arguments to pass to java.Thanks!
  • user3640967
    user3640967 almost 8 years
    Argh! This has finally stopped my Jenkins installation from crashing after 3 days of hunting for the problem (thankyou!) But why on Earth is the Java default still just 256MB in 2016???
  • user1053510
    user1053510 almost 7 years
    On my openSUSE Leap 42.1 (x86_64) the Jenkins config for version 2.70 is located in /etc/sysconfig/jenkins
  • carl verbiest
    carl verbiest about 6 years
    also for RedHat RHEL 7.4, jenkins 2.98
  • Assi.NET
    Assi.NET over 4 years
    You can also say -Xmx8g which is the same as -Xmx8384m, but more easy.
  • Assi.NET
    Assi.NET over 4 years
    You can also say -Xmx8g which is the same as -Xmx8384m, but more easy.
  • SaundersB
    SaundersB over 4 years
    Wouldn't 1GB be 1024MB?
  • FooBar
    FooBar over 3 years
    I don't see this setting anywhere, maybe you should describe more in detail where you found it.
  • Gank
    Gank over 3 years
    [root@Berder-S-01 ~]# service jenkins restart Restarting jenkins (via systemctl): Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details. [FAILED]
  • VanagaS
    VanagaS almost 3 years
    @FooBar Those options come from environment injector plugin