Java 为什么我的 Gradle 构建会因退出代码 137 而死亡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38967991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are my Gradle builds dying with exit-code 137?
提问by Mridang Agarwalla
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 memory issue. If I reduce the number of tests in the suite, it runs fine.
我一直在尝试编译和测试一个大型项目以使用 Gradle。测试运行良好,直到他们意外死亡。我四处挖掘,资源说这是由于内存问题。如果我减少套件中的测试数量,它运行良好。
I increased the memory by 4x increased the debugging level but I still don't follow what causes this. Here's the horribly cryptic stacktrace. The last line (scroll right) shows the memory settings that I've defined.
我将内存增加了 4 倍,提高了调试级别,但我仍然不明白是什么原因造成的。这是可怕的神秘堆栈跟踪。最后一行(向右滚动)显示了我定义的内存设置。
...
...
...
1125 tests completed, 30 failed, 9 skipped
:test FAILED
:test (Thread[Daemon worker,5,main]) completed. Took 8 mins 39.684 secs.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> Process 'Gradle Test Executor 1' finished with non-zero exit value 137
* Try:
Run with --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':test'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185)
at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66)
at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110)
at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
at org.gradle.execution.DefaultBuildExecuter.access./gradlew build --no-daemon
0(DefaultBuildExecuter.java:23)
at org.gradle.execution.DefaultBuildExecuter.proceed(DefaultBuildExecuter.java:43)
at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:154)
at org.gradle.internal.Factories.create(Factories.java:22)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:151)
at org.gradle.initialization.DefaultGradleLauncher.access0(DefaultGradleLauncher.java:32)
at org.gradle.initialization.DefaultGradleLauncher.create(DefaultGradleLauncher.java:99)
at org.gradle.initialization.DefaultGradleLauncher.create(DefaultGradleLauncher.java:93)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82)
at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94)
at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:78)
at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:48)
at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:52)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput.call(ForwardClientInput.java:74)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput.call(ForwardClientInput.java:72)
at org.gradle.util.Swapper.swap(Swapper.java:38)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:40)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:72)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy.run(StartBuildOrRespondWithBusy.java:50)
BUILD FAILED at org.gradle.launcher.daemon.server.DaemonStateCoordinator.run(DaemonStateCoordinator.java:246)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl.run(StoppableExecutorImpl.java:40)
Caused by: org.gradle.process.internal.ExecException: Process 'Gradle Test Executor 1' finished with non-zero exit value 137
Total time: 9 mins 38.624 secs
at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:367)
at org.gradle.process.internal.DefaultWorkerProcess.waitForStop(DefaultWorkerProcess.java:161)
at org.gradle.api.internal.tasks.testing.worker.ForkingTestClassProcessor.stop(ForkingTestClassProcessor.java:86)
at org.gradle.api.internal.tasks.testing.processors.RestartEveryNTestClassProcessor.endBatch(RestartEveryNTestClassProcessor.java:60)
at org.gradle.api.internal.tasks.testing.processors.RestartEveryNTestClassProcessor.stop(RestartEveryNTestClassProcessor.java:54)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.FailureHandlingDispatch.dispatch(FailureHandlingDispatch.java:29)
at org.gradle.messaging.dispatch.AsyncDispatch.dispatchMessages(AsyncDispatch.java:132)
at org.gradle.messaging.dispatch.AsyncDispatch.accesstest {
jvmArgs "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap"
}
0(AsyncDispatch.java:33)
at org.gradle.messaging.dispatch.AsyncDispatch.run(AsyncDispatch.java:72)
... 2 more
Stopped 0 compiler daemon(s).
Received result Failure[value=org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: Execution failed for task ':test'.] from daemon DaemonInfo{pid=48, address=[65adcc0f-8881-4511-b7a0-1b88c7016510 port:34390, addresses:[/0:0:0:0:0:0:0:1%lo, /127.0.0.1]], idle=false, context=DefaultDaemonContext[uid=14546872-57d6-4da6-8358-dc41cb7bb01d,javaHome=/usr/lib/jvm/java-8-oracle,daemonRegistryDir=/srv/myapplication/.gradle/daemon,pid=48,idleTimeout=120000,daemonOpts=-XX:MaxMetaspaceSize=320m,-XX:+DisableExplicitGC,-XX:+UseConcMarkSweepGC,-XX:NewSize=124m,-XX:SurvivorRatio=16,-Xms257m,-Xmx2049m,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant]} (build should be done).
采纳答案by Mridang Agarwalla
This issue seems to be related to Linux rather than Gradle as stated in the Jenkins docs:
这个问题似乎与 Linux 相关,而不是 Jenkins 文档中所述的 Gradle:
In cases where virtual memory is running short the kernel OOM (Out of Memory) killer may forcibly kill Jenkins or individual builds. If this occurs on Linux you may see builds terminate with exit code 137 (128 + signal number for SIGKILL). The
dmesg
command output will show log messages that will confirm the action that the kernel took.
在虚拟内存不足的情况下,内核 OOM(内存不足)杀手可能会强制终止 Jenkins 或个人构建。如果在 Linux 上发生这种情况,您可能会看到构建以退出代码 137(128 + SIGKILL 的信号编号)终止。该
dmesg
命令的输出将表明,将确认内核采取的操作日志消息。
https://wiki.jenkins-ci.org/display/JENKINS/I'm+getting+OutOfMemoryError
https://wiki.jenkins-ci.org/display/JENKINS/I'm+getting+OutOfMemoryError
回答by Roman Golyshev
I've had similar issue on DigitalOcean's server, my gradle build failed completely on test
stage with very similar stacktrace and without a single test being executed.
我在 DigitalOcean 的服务器上遇到了类似的问题,我的 gradle 构建在test
舞台上完全失败,堆栈跟踪非常相似,并且没有执行单个测试。
It is stated in Gradle docs that gradle daemon should not be run in CI environments. So I just added --no-daemon
to my build command and everything worked well and good. Also stopping daemon with ./gradlew --stop
has been useful, but it worked only for a single build - the next one also failed.
Gradle 文档中声明 gradle 守护进程不应在 CI 环境中运行。所以我只是添加--no-daemon
到我的构建命令中,一切都运行良好。停止守护进程./gradlew --stop
也很有用,但它仅适用于单个构建 - 下一个构建也失败了。
My build command:
我的构建命令:
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)
回答by Baptiste Mesta
I had the same issue but in a CI environment where build are launched in dockercontainer. In that specific case the JVM is not aware of how much memory it can use and you can experience this type of issues.
我遇到了同样的问题,但在 CI 环境中,构建是在docker容器中启动的。在这种特定情况下,JVM 不知道它可以使用多少内存,您可能会遇到此类问题。
In order to let the JVM know how much memory is available, you can use
为了让JVM知道有多少内存可用,可以使用
gradle build -Dorg.gradle.jvmargs=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap
gradle build -Dorg.gradle.jvmargs=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap
and also set it on your test tasks:
并将其设置为您的测试任务:
test {
maxHeapSize '512m'
forkEvery 100
jvmArgs '-Xmx512m', '-Xms512m'
}
This is a new JVM feature introduced in 8u131+
这是 8u131+ 中引入的新 JVM 特性
see: http://royvanrijn.com/blog/2018/05/java-and-docker-memory-limits/
见:http: //royvanrijn.com/blog/2018/05/java-and-docker-memory-limits/
回答by Little Mike
I've also been having the same problem on CircleCI, but I didn't have any luck with any of the above. This is what I found:
我在 CircleCI 上也遇到了同样的问题,但我对上述任何一项都没有任何运气。这是我发现的:
- Adding
-Dorg.gradle.daemon=false
to my CircleCIconfig.yml
stopped the daemon from being used, but didn't fix the problem. - Adding
-Dorg.gradle.workers.max=2
toGRADLE_OPTS
, or--max-workers 2
to the gradle command didn't seem to have much/any effect from what I could see. I tried--max-workers=2
just in case, because both formats seem to be floating around on Google. I connected to my CircleCI container, and intop
I could still see 3-4 Java processes forking off, so not sure this is doing anything? - I also tried max workers = 1 in the combinations above.
- Tried
-XX:+UnlockExperimentalVMOptions
and-XX:+UseCGroupMemoryLimitForHeap
in both JVM args, and in thetest {}
configuration inside my build as suggested by Baptiste Mesta. I don't see how this could work; I would have thought the multiple forked processes don't know what proportion of the container memory the other processes are using up? Unless I'm not understanding it correctly.
- 添加
-Dorg.gradle.daemon=false
到我的 CircleCIconfig.yml
阻止了守护进程的使用,但没有解决问题。 - 添加
-Dorg.gradle.workers.max=2
到GRADLE_OPTS
, 或--max-workers 2
gradle 命令似乎对我所看到的没有太大/任何影响。我试过--max-workers=2
以防万一,因为这两种格式似乎都在谷歌上流传。我连接到我的 CircleCI 容器,top
我仍然可以看到 3-4 个 Java 进程分叉,所以不确定这有什么作用? - 我还在上面的组合中尝试了 max workers = 1。
- 按照 Baptiste Mesta 的建议
-XX:+UnlockExperimentalVMOptions
,-XX:+UseCGroupMemoryLimitForHeap
在 JVM 参数和test {}
我的构建中的配置中都进行了尝试。我不明白这是如何工作的;我会认为多个分叉进程不知道其他进程正在使用的容器内存比例是多少?除非我没有正确理解它。
In the end, I fixed it just by being nice and explicit with the memory settings, rather than using magic:
最后,我只是通过对内存设置的友好和明确来修复它,而不是使用魔法:
- Circle CI config:
GRADLE_OPTS: -Xmx2048m -Dorg.gradle.daemon=false
- Gradle build:
test { maxHeapSize = "512m" }
- 圆形 CI 配置:
GRADLE_OPTS: -Xmx2048m -Dorg.gradle.daemon=false
- 摇篮构建:
test { maxHeapSize = "512m" }
Edit: You may need to go lower, depending on whether you have other processes running.
编辑:您可能需要降低,具体取决于您是否有其他进程正在运行。
回答by k6ps
I had a similar issue with Bamboo agents (running on Docker) running Gradle 4.6. The test
task just abruptly terminated with
我在运行 Gradle 4.6 的 Bamboo 代理(在 Docker 上运行)遇到了类似的问题。该test
任务只是突然有终止
android {
testOptions {
unitTests {
returnDefaultValues = true
includeAndroidResources = true
all {
maxHeapSize = "1024m"
}
}
}
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:
我们有超过 3000 个单元测试。在我们的例子中,这个问题是通过forkEvery
限制每个测试执行程序进程运行的测试类的数量来解决的,并且还限制了内存使用:
./gradlew test --no-daemon -Dorg.gradle.jvmargs=-XX:+UseContainerSupport
回答by Mark Gilchrist
If you are having trouble with this on CircleCi, where you are trying to run Robolectric Test
如果您在尝试运行 Robolectric 测试的 CircleCi 上遇到此问题
I found the solution to my problems on their documentation page for android
我在他们的 android 文档页面上找到了我的问题的解决方案
https://circleci.com/docs/2.0/language-android/
https://circleci.com/docs/2.0/language-android/
which was simply to add
这只是为了添加
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
如果您仍然遇到 OOM 问题,您还可以限制 gradle 的最大工人数:./gradlew test --max-workers 4
回答by Max Mao
Similar to Baptiste Mesta's answer but for JDK 11 I used the UseContainerSupport JVM option to solve my Jenkins build.
类似于 Baptiste Mesta 的回答,但对于 JDK 11,我使用了 UseContainerSupport JVM 选项来解决我的 Jenkins 构建。
##代码##回答by Zia
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
这实际上是一个内存问题,通常,Docker 容器的内存限制为 4G,因此您需要注意 Java 堆不会超过该限制,解决方案很少,我参考了 android
Add this to gradle.properties (change size according to your need)
org.gradle.jvmargs=-Xmx10248m -XX:MaxPermSize=256m
Add this to your build.gradle
android.testOptions.unitTests.all { maxHeapSize = "1024m" }
将此添加到 gradle.properties (根据需要更改大小)
org.gradle.jvmargs=-Xmx10248m -XX:MaxPermSize=256m
将此添加到您的 build.gradle
android.testOptions.unitTests.all { maxHeapSize = "1024m" }
or
或者
##代码##If you are still running into OOM issues you can also limit the max workers for gradle: ./gradlew test --max-workers 4
如果您仍然遇到 OOM 问题,您还可以限制 gradle 的最大工人数: ./gradlew test --max-workers 4
Hope this helps.
希望这可以帮助。