Why are my Gradle builds dying with exit-code 137?

后端 未结 9 2030
醉酒成梦
醉酒成梦 2020-12-07 23:48

I\'ve been trying to compile and test a large project to use Gradle. The test run fine until they die unexpectedly. I dug around and resources said that this is due to a mem

相关标签:
9条回答
  • 2020-12-08 00:25

    This is actually a memory issue, Generally, Docker container has a memory limit of 4G so you need to take care that your java heap doesn't cross that limit, there are few solutions to it I am taking reference of android

    1. Add this to gradle.properties (change size according to your need)

      org.gradle.jvmargs=-Xmx10248m -XX:MaxPermSize=256m

    2. Add this to your build.gradle

      android.testOptions.unitTests.all { maxHeapSize = "1024m" }

    or

    android {
        testOptions {
            unitTests {
                // Any other configurations
                all {
                    maxHeapSize = "1024m"
                }
            }
        }
    

    If you are still running into OOM issues you can also limit the max workers for gradle: ./gradlew test --max-workers 4

    Hope this helps.

    0 讨论(0)
  • 2020-12-08 00:27

    I had the same issue but in a CI environment where build are launched in docker container. In that specific case the JVM is not aware of how much memory it can use and you can experience this type of issues.

    In order to let the JVM know how much memory is available, you can use

    gradle build -Dorg.gradle.jvmargs=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap

    and also set it on your test tasks:

    test {
      jvmArgs "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap"
    }
    

    This is a new JVM feature introduced in 8u131+

    see: http://royvanrijn.com/blog/2018/05/java-and-docker-memory-limits/

    0 讨论(0)
  • 2020-12-08 00:27

    I'd similar issue on intellij. I upgraded the memory as stated in above answers and disabled daemon in gradle.properties file

    org.gradle.daemon=false
    

    Gradle Daemon is a long lived build process. It caches the information about files, tasks,project structure, etc from the previous build. Hence, making your subsequent builds efficient. Daemon process is enabled by default. But sometimes it gets corrupted, so you may try disabling daemon process.

    0 讨论(0)
  • 2020-12-08 00:35

    If you are having trouble with this on CircleCi, where you are trying to run Robolectric Test

    I found the solution to my problems on their documentation page for android

    https://circleci.com/docs/2.0/language-android/

    which was simply to add

    android {
        testOptions {
            unitTests {
                returnDefaultValues = true
                includeAndroidResources = true
    
                all {
                    maxHeapSize = "1024m"
                }
            }
        }
    

    If you are still running into OOM issues you can also limit the max workers for gradle: ./gradlew test --max-workers 4

    0 讨论(0)
  • 2020-12-08 00:45

    Similar to Baptiste Mesta's answer but for JDK 11 I used the UseContainerSupport JVM option to solve my Jenkins build.

    ./gradlew test --no-daemon -Dorg.gradle.jvmargs=-XX:+UseContainerSupport
    
    0 讨论(0)
  • 2020-12-08 00:46

    I had a similar issue with Bamboo agents (running on Docker) running Gradle 4.6. The test task just abruptly terminated with

        build   27-Dec-2018 22:00:20    22:00:20.018 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Process 'Gradle Test Executor 1' finished with exit value 137 (state: FAILED)
    

    We have over 3000 unit tests. In our case, the issue was solved using forkEvery to limit the number of test classes run per test executor process, and also limiting memory usage:

        test {
            maxHeapSize '512m'
            forkEvery 100
            jvmArgs '-Xmx512m', '-Xms512m'
        }
    
    0 讨论(0)
提交回复
热议问题