133

I want to increase the available heap space for Jenkins. But as it is installed as a service I don´t know how to do it.

trincot
  • 317,000
  • 35
  • 244
  • 286
Deleted
  • 4,067
  • 6
  • 33
  • 51
  • 1
    See also http://stackoverflow.com/questions/14762162/how-do-i-give-jenkins-more-heap-space-when-its-running-as-a-daemon-on-ubuntu – Mark Butler Mar 15 '13 at 00:23

5 Answers5

121

If you used Aptitude (apt-get) to install Jenkins on Ubuntu 12.04, uncomment the JAVA_ARGS line in the top few lines of /etc/default/jenkins:

# arguments to pass to java
#JAVA_ARGS="-Xmx256m"   # <--default value
JAVA_ARGS="-Xmx2048m"
#JAVA_ARGS="-Djava.net.preferIPv4Stack=true" # make jenkins listen on IPv4 address
Steve HHH
  • 12,947
  • 6
  • 68
  • 71
  • 5
    what if it's windows? – Dejell Dec 08 '14 at 12:20
  • 8
    Question specifically states Windows. – Nick Udell Dec 17 '14 at 14:37
  • 7
    Yeah, I wrote this answer before I knew what I was doing on SO, and I'm surprised that it's gotten so many upvotes. Perhaps, like me, a lot of people find their way to this question through a search engine, and a larger percentage of those people are using Linux or Unix? I don't understand it. – Steve HHH Dec 17 '14 at 17:18
  • 9
    "Perhaps, like me, a lot of people find their way to this question through a search engine, and a larger percentage of those people are using Linux or Unix? I don't understand it." That's exactly how I found it. – Scott Aug 03 '15 at 13:37
  • 1
    Don't forget restart jenkins service `sudo service jenkins stop` `sudo service jenkins start` – Camilo Silva Aug 31 '15 at 14:55
  • We should edit the question to include linux servers, as the majority of servers run some form of linux OS, and not windows, and since this answer has the most up votes. – Jordan Stewart Jan 30 '17 at 01:27
  • /etc/sysconfig/jenkins for centos 7 – technocrat May 07 '18 at 19:08
  • Warning! After Jenkins 2.332.1 the `/etc/default/jenkins` config file is ignored: https://stackoverflow.com/questions/71494231/jenkins-changes-in-etc-default-jenkins-not-working – Martin Vysny Feb 04 '23 at 09:00
103

In your Jenkins installation directory there is a jenkins.xml, where you can set various options. Add the parameter -Xmx with the size you want to the arguments-tag (or increase the size if its already there).

dunni
  • 43,386
  • 10
  • 104
  • 99
  • 1
    see also https://stackoverflow.com/questions/14762162/how-do-i-give-jenkins-more-heap-space-when-its-running-as-a-daemon-on-ubuntu – user817795 Aug 28 '17 at 03:40
  • 2
    my question is : if the builds is running out of memory , why we should set the java options in the master ? – yarin Jan 24 '18 at 08:26
  • 2
    You shouldn't. But this question was not about builds running out of memory, it was about master running out of memory. – dunni Jan 24 '18 at 08:41
  • 1
    As of Ubuntu 16.04.6 LTS, there's no such file. The `/etc/default/jenkins` solution offered below by Steve is the one that works for me. – insideClaw Jan 24 '20 at 11:14
  • So if the build step of a jenkins job is failing with this error, then java opts should be set for the jenkins SLAVE, right? – Azee77 Feb 15 '23 at 04:50
78

You need to modify the jenkins.xml file. Specifically you need to change

   <arguments>-Xrs -Xmx256m 
    -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle 
    -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>

to

    <arguments>-Xrs -Xmx2048m -XX:MaxPermSize=512m 
    -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle 
    -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>

You can also verify the Java options that Jenkins is using by installing the Jenkins monitor plugin via Manage Jenkins / Manage Plugins and then navigating to Managing Jenkins / Monitoring of Hudson / Jenkins master to use monitoring to determine how much memory is available to Jenkins.

If you are getting an out of memory error when Jenkins calls Maven, it may be necessary to set MAVEN_OPTS via Manage Jenkins / Configure System e.g. if you are running on a version of Java prior to JDK 1.8 (the values are suggestions):

-Xmx2048m  -XX:MaxPermSize=512m

If you are using JDK 1.8:

-Xmx2048m
Mark Butler
  • 4,361
  • 2
  • 39
  • 39
  • when i try setting the JENKINS_JAVA_OPTIONS as described above i get: "Starting Jenkins Unrecognized option: --XX:MaxPermSize=512m" – nemoo Jul 30 '12 at 14:38
  • 3
    There should be only one hyphen: `-XX:MaxPermSize=512m` - the above response has been edited to fix this typo. – Adam Rofer Aug 23 '12 at 20:59
  • 1
    Any idea how to set JENKINS_JAVA_OPTIONS in Windows? – ATOzTOA Mar 14 '13 at 06:57
  • On Windows, according to http://jenkins.361315.n4.nabble.com/How-set-JENKINS-JAVA-OPTIONS-under-Windows-Exception-URI-must-start-with-a-slash-td3426304.html you use Control Panel -> System -> Advanced -> Environment Variables – Mark Butler Mar 15 '13 at 00:26
  • 2
    `-XX:MaxPermSize` is no longer used with Java 8 or higher https://stackoverflow.com/questions/12114174/what-does-xxmaxpermsize-do – Micha Wiedenmann Feb 09 '16 at 08:15
38

I've added to /etc/sysconfig/jenkins (CentOS):

# Options to pass to java when running Jenkins.
#
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Xmx1024m -XX:MaxPermSize=512m"

For ubuntu the same config should be located in /etc/default

ipeacocks
  • 2,187
  • 3
  • 32
  • 47
16

From the Jenkins wiki:

The JVM launch parameters of these Windows services are controlled by an XML file jenkins.xml and jenkins-slave.xml respectively. These files can be found in $JENKINS_HOME and in the slave root directory respectively, after you've install them as Windows services.

The file format should be self-explanatory. Tweak the arguments for example to give JVM a bigger memory.

https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service

Isaac Truett
  • 8,734
  • 1
  • 29
  • 48