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

前端 未结 4 1272
名媛妹妹
名媛妹妹 2020-12-02 13:27

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

相关标签:
4条回答
  • 2020-12-02 13:39

    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.

    0 讨论(0)
  • 2020-12-02 13:41

    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:

    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:

    0 讨论(0)
  • 2020-12-02 13:46

    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="

    0 讨论(0)
  • 2020-12-02 13:46

    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"
    
    

    0 讨论(0)
提交回复
热议问题