Michiyo
Michiyo

Reputation: 1201

Increasing PermGen space for Jenkins/gradle

I am running a Jenkins build of an Android project on a Mac Mini (10.9.5). The Jenkins build is failing with error messages like this:

<package>.myTest > test_myTest FAILED
org.mockito.cglib.core.CodeGenerationException at test_myTest.java:65
Caused by: java.lang.reflect.InvocationTargetException at test_myTest.java:65
Caused by: java.lang.OutOfMemoryError at test_myTest.java:65
java.lang.OutOfMemoryError: PermGen space

This is sometimes followed by messages like

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "/0:0:0:0:0:0:0:1:50340 to /0:0:0:0:0:0:0:1:50339 workers Thread 2"
16:47:17 
16:47:17 Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "/0:0:0:0:0:0:0:1:50340 to /0:0:0:0:0:0:0:1:50339 workers Thread 4"
16:47:18 
16:47:18 Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "/0:0:0:0:0:0:0:1:50340 to /0:0:0:0:0:0:0:1:50339 workers Thread 5"
16:47:18 
16:47:18 Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "/0:0:0:0:0:0:0:1:50340 to /0:0:0:0:0:0:0:1:50339 workers Thread 6"
16:47:19 

It usually fails at the same point in the build. According to the Jenkins wiki

Do you consistently see OOME around the same phase in a build? If so, maybe it just needs a bigger memory.

this may mean I just need more PermGen space.

The stackoverflow posts/blog posts I've read indicate that I need to increase the max PermGen size (-XX:MaxPermSize=1024M, for example). However, I'm not clear on where to do this.

I've changed this for GRADLE_OPTS and JAVA_OPTS so my Jenkins build environment looks like this: environmental variables

As seen in the screenshot, I also added some options to garbage collect Perm Gen as recommended here.

This seemed to be working--I had a few successful builds yesterday, but it's now failing again (with no changes that I'm aware of).

After reading this answer, I also changed the following line in my project's gradle.properties file.

org.gradle.jvmargs=-Xms1024M -Xmx2048M -XX:PermSize=512M -XX:MaxPermSize=2048 -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

This hasn't fixed the problem.

Answers to similar questions like this and this make me think I may be approaching this the wrong way--should I be changing a computer setting for the Mac (10.9.5) which is running Jenkins? What is the correct way to modify the PermGen space?

Edit: I had previously thought that perhaps the environmental variables weren't being set, but I verified that they appear under the build result Environmental Variables (jenkins/job/<Project>/146/injectedEnvVars/)

java_opts gradle_opts

Upvotes: 1

Views: 2834

Answers (2)

Michiyo
Michiyo

Reputation: 1201

As Integrating Stuff said, it was necessary to increase the MaxPermSize for the unit tests. I found how to do so here in the "Running from Gradle" section.

android {
    testOptions {
        unitTests.all {
            jvmArgs '-XX:MaxPermSize=1024m' //prevent OOM (PermGen space) while running tests
        }
    }
    ...
}

Upvotes: 1

Integrating Stuff
Integrating Stuff

Reputation: 5303

The memory settings are set per running JVM process. So you definitely do not need to set any OS variables or system wide settings to fix this.

You first need to determine which process is throwing the error. I am assuming it is the Gradle build and not the Jenkins itself. It it is the Gradle build, you still do not know which process.

By default, the Gradle Test task executes the tests in a separate jvm, so changing the values in gradle.properties will not help you in such a case. In such case, you need to configure your Gradle Test task correctly, for example:

test {
    jvmArgs "-XX:MaxPermSize=2048m"
}

Upvotes: 0

Related Questions