Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many threads should Jenkins run?

Tags:

jenkins

I have a Jenkins server that keeps running out of memory and cannot create native threads. I've upped the memory and installed the Monitoring plugin.

There are about 150 projects on the server, and I've been watching the thread count creep up all day. It is now around 990. I expect when it hits 1024, which is the user limit for threads, Jenkins will run out of memory again.

[edit]: I have hit 1016 threads and am now getting the out of memory error

Is this an appropriate number of threads for Jenkins to be running? How can I tell Jenkins to destroy threads when it is finished with them?

like image 261
CamelBlues Avatar asked Jul 02 '12 20:07

CamelBlues


People also ask

How to increase Jenkins threads?

In Jenkins there is an option in Manage Jenkins > Configure System which allows users to manually set the number of polling threads to any number. This option is only available to Jenkins installations with over 10 jobs which all are using polling.

How many threads should I allocate?

General rule of thumb for threading an application: 1 thread per CPU Core. On a quad core PC that means 4. As was noted, the XBox 360 however has 3 cores but 2 hardware threads each, so 6 threads in this case.

How reduce Jenkins memory usage?

One is to make sure you're rotating your build history or discarding your old builds. That helps reduce Jenkins' memory footprint, which improves performance. We recommend keeping 30 to 60 days of build history, but you should configure this on a per-job basis.

What is thread dump in Jenkins?

During the troubleshooting of Jenkins, others may request that you obtain thread dumps of relevant Java VMs. Thread dumps concisely capture what every thread in a VM is doing at a given point in time. They are useful to diagnose hang problems, deadlocks, and performance issues.


2 Answers

tl;dr:

There was a post-build action running a bash script that didn't return anything via stderr or stdout to Jenkins. Therefore, every time the build ran, threads would be created and get stuck waiting. I resolved this issue by having the bash script return an exit status.

long answer

I am running Jenkins on CentOS and have installed in via the RPM. In terms of modifying the Winstone servlet container, you can change that in Jenkin's init script in /etc/sysctrl/jenkins. However the options above only control the number of HTTP threads that are created, not the number of threads overall.

That would be a solution if my threads were hanging on accessing an HTTP API of Jenkins as part of a post-commit action. However, using the ever-handy Monitoring plugin mentioned in my question, I inspected the stuck threads.

The threads were stuck on something in the com.trilead.ssh2.channel package. The getChannelData method has a while(true) loop that looks for output on the stderr or stdout of an ssh stream. The thread was getting suck in that loop because nothing was coming through. I learned this on GrepCode.

This was because the post-build action was to execute a command via SSH onto a server and execute a bash script that would inspect a git repo. However, the git repo was misconfigured and the git command would error, but the exit 1 status did not bubble up through the bash script (partially due to a misformed if-elif-else statement).

The script completed and the build was considered a success, but somehow the thread handling the SSH connection from Jenkins was left hanging due to this git error.

But thank you for your help on this question!

like image 55
CamelBlues Avatar answered Oct 03 '22 07:10

CamelBlues


If you run Jenkins "out of the box" it uses Winstone servlet container. You can pass command-line arguments to it as described here. Some of those parameters can limit the number of threads:

 --handlerCountStartup    = set the no of worker threads to spawn at startup. Default is 5
 --handlerCountMax        = set the max no of worker threads to allow. Default is 300
 --handlerCountMaxIdle    = set the max no of idle worker threads to allow. Default is 50

Now, I tried this some time ago and was not 100% convinced that it worked, so no guarantees, but it is worth a try.

like image 30
malenkiy_scot Avatar answered Oct 03 '22 09:10

malenkiy_scot