简体   繁体   English

当 Jenkins 作为守护进程在 Ubuntu 上运行时,如何给它更多的堆空间?

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

My Jenkins jobs are running out of memory, giving java.lang.OutOfMemoryError messages in the build log.我的 Jenkins 作业内存不足,在构建日志中给出java.lang.OutOfMemoryError消息。 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.但是我使用了 Ubuntu 包管理器、 aptitudeapt-get来安装 Jenkins,但我不知道在哪里可以更改分配给 Jenkins 的堆空间量。

There are two types of OutOfMemoryError messages that you might encounter while a Jenkins job runs:在 Jenkins 作业运行时,您可能会遇到两种类型的OutOfMemoryError消息:

  • 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: Heap space – 这意味着您需要在守护进程启动时增加分配给 Jenkins 的堆空间量。
  • java.lang.OutOfMemoryError: PermGen space – this means you need to increase the amount of generation space allocated to store Java object metadata. java.lang.OutOfMemoryError: PermGen space – 这意味着您需要增加分配给存储 Java 对象元数据的生成空间量。 Increasing the value of the -Xmx parameter will have no affect on this error.增加-Xmx参数的值不会影响此错误。

On Ubuntu 12.04 LTS, uncomment the JAVA_ARGS setting on line ten of /etc/default/jenkins :在 Ubuntu 12.04 LTS 上,取消注释/etc/default/jenkins行的JAVA_ARGS设置:

  • To add more Java heap space, increase the value of the -Xmx Java parameter.要添加更多 Java 堆空间,请增加-Xmx Java 参数的值。 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.要添加更多 PermGen 空间,请添加参数XX:MaxPermSize=512m (如果需要更多,请将512替换为其他内容。永久代堆保存有关用户类的元信息。

For example, this extract is from the default /etc/default/jenkins after a fresh install of Jenkins:例如,此摘录来自全新安装 Jenkins 后的默认/etc/default/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:如果您将堆空间设置为 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).注意不要将堆大小设置得太大,因为无论您分配什么都会减少操作系统和其他程序可用的内存量,这可能会导致过度分页(内存在 RAM 和交换磁盘之间来回交换,这会减慢您的系统已关闭)。

If you also set MaxPermSpace , you need to add a space between the parameters):如果您还设置了MaxPermSpace ,则需要在参数之间添加一个空格):

# 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 .进行更改后,从 Jenkins Web 界面优雅地重新启动 Jenkins,或使用sudo /etc/init.d/jenkins restart从命令行强制立即重新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 .我发现以下站点对于理解 Java 最大和永久代堆大小很有用: http : //www.freshblurbs.com/blog/2005/05/19/explaining-java-lang-outofmemoryerror-permgen-space.html

对于 CentOS,Jenkins.xml 所在的目录默认为 /etc/sysconfig/,对于 jenkins-1.579-1.1

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

If you are using Ubuntu Server, first install Monitoring plugin to see how much memory Jenkins is using.如果您使用的是 Ubuntu Server,请先安装Monitoring插件以查看 Jenkins 使用了多少内存。 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.然后,使用命令free -m ,我弄清楚服务器的内存大小是多少。 In my case, 16Gb.就我而言,16Gb。 With that info, I opened /etc/default/jenkins and changed:有了这些信息,我打开了/etc/default/jenkins并更改了:

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

to

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

Where 8384 is 8Gb.其中 8384 是 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:然后我使用命令sudo service jenkins restart重新启动 Jenkins,然后,在触发出现内存问题的作业后,事情看起来好多了,作业可以在本次和后续运行中完成:

在此处输入图片说明

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"

詹金斯工作配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM