How to configure Jenkins with Nginx to avoid "Cannot allocate memory" issue

Posted July 5, 2017 4.4k views
NginxDeploymentUbuntu 16.04

I have Jenkins running behind a Nginx proxy on my Ubuntu 16.04 server. I have connected it with GitHub through webhooks, and the connection works perfectly, however every build fails, and I keep getting the same error log:

Started by user admin
Building in workspace /var/lib/jenkins/workspace/
[WS-CLEANUP] Deleting project workspace...
Cloning the remote Git repository
Cloning repository
 > /usr/bin/git init /var/lib/jenkins/workspace/ # timeout=10
Fetching upstream changes from
 > /usr/bin/git --version # timeout=10
 > /usr/bin/git fetch --tags --progress +refs/heads/*:refs/remotes/origin/* # timeout=15
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags --progress +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stderr: error: cannot fork() for fetch-pack: Cannot allocate memory

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(
    at hudson.plugins.git.GitSCM.retrieveChanges(
    at hudson.plugins.git.GitSCM.checkout(
    at hudson.scm.SCM.checkout(
    at hudson.model.AbstractProject.checkout(
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(
    at jenkins.scm.SCMCheckoutStrategy.checkout(
    at hudson.model.AbstractBuild$
    at hudson.model.Run.execute(
    at hudson.model.ResourceController.execute(
ERROR: Error cloning remote repo 'origin'
Finished: FAILURE

I can’t figure out what is going on, because everything seems to work fine. The strange thing is that if I log in to my server as jenkins and I perform these steps manually, everything succeeds.

In addition, Jenkins breaks occasionally and from that point Nginx keeps giving me a 503 error. If I restart the Jenkins service, everything works again.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Turned out it was a simple OOM (out-of-memory) problem, which I could easily resolve by creating a swap file. To do so, I have followed this guide:

I have also made some notes, if you are having similar issues you might find it helpful:

by Justin Ellingwood
Swap space can be used as an "overflow" area for your system when you run out of RAM. The operating system can store data that would normally be kept in RAM on the hard drive in a specially formatted file. In this guide, we'll demonstrate how to create and use one of these files in Ubuntu 14.04.