在 Gradle Proguard 优化时出现大量内存错误

Posted

技术标签:

【中文标题】在 Gradle Proguard 优化时出现大量内存错误【英文标题】:Getting tons of memory errors while Gradle Proguard optimizing 【发布时间】:2017-12-20 12:16:20 【问题描述】:

我为我的 Java 应用程序运行 ./gradlew proguard --debug 以检查为什么它没有在几个小时内完成,这就是我所看到的:

17:14:11.431 [INFO] [system.out] Optimizing...
17:14:13.537 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event Total: 8589934592, Free: 2029916160
17:14:13.538 [DEBUG] [org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: Total: 8589934592, Free: 2029916160
17:14:13.538 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event Maximum: 954728448, Committed: 580386816
17:14:18.535 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event Total: 8589934592, Free: 2014097408
17:14:18.535 [DEBUG] [org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: Total: 8589934592, Free: 2014097408
17:14:18.535 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event Maximum: 954728448, Committed: 578289664
17:14:20.726 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] DaemonExpirationPeriodicCheck running
17:14:20.727 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
17:14:20.727 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
17:14:20.727 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
17:14:20.727 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
17:14:20.727 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
17:14:20.728 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
17:14:23.538 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event Total: 8589934592, Free: 1980108800
17:14:23.538 [DEBUG] [org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: Total: 8589934592, Free: 1980108800
17:14:23.538 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event Maximum: 954728448, Committed: 534773760
17:14:28.536 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event Total: 8589934592, Free: 2000781312
17:14:28.536 [DEBUG] [org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: Total: 8589934592, Free: 2000781312
17:14:28.537 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event Maximum: 954728448, Committed: 575668224
17:14:30.725 [DEBUG] [org.gradle.launcher.daemon.server.Daemon] DaemonExpirationPeriodicCheck running
17:14:30.725 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
17:14:30.725 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
17:14:30.726 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
17:14:30.726 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire shared lock on daemon addresses registry.
17:14:30.726 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on daemon addresses registry.
17:14:30.726 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Releasing lock on daemon addresses registry.
17:14:33.535 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event Total: 8589934592, Free: 1986662400
17:14:33.536 [DEBUG] [org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: Total: 8589934592, Free: 1986662400
17:14:33.536 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event Maximum: 954728448, Committed: 570425344
17:14:38.539 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting OS memory status event Total: 8589934592, Free: 1962721280
17:14:38.539 [DEBUG] [org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy] Received memory status update: Total: 8589934592, Free: 1962721280
17:14:38.539 [DEBUG] [org.gradle.process.internal.health.memory.MemoryManager] Emitting JVM memory status event Maximum: 954728448, Committed: 571473920

我认为这些错误可能是原因,因为它们每秒都会被打印出来。我尝试在 gradle.properties 中设置它,但没有改变:

GRADLE_OPTS = "-Xmx2048m -XX:MaxPermSize=2048m"

【问题讨论】:

我也遇到了这个问题。你找到解决办法了吗? 我也想知道。 【参考方案1】:

要配置 Gradle JVM 内存,您需要在 gradle.properties 中设置 org.gradle.jvmargs,如 Build Environment / Configuring JVM memory 中所述

文档中的示例:

org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

GRADLE_OPTS 不适合你,因为:

GRADLE_OPTS 是环境变量,不是 gradle.property 配置,因此放入 gradle.properties 中没有任何作用。它需要在启动 Gradle 之前设置。例如,像这样:
export GRADLE_OPTS="some opts here"
gradle build
根据 Gradle 文档,GRADLE_OPTS 仅配置 Gradle 客户端,但您的构建很可能在 Gradle 守护程序中运行。

更多详情请见Build Environment / Environment variables。

【讨论】:

以上是关于在 Gradle Proguard 优化时出现大量内存错误的主要内容,如果未能解决你的问题,请参考以下文章

尝试生成签名 Apk 时出现 Gradle 构建错误

Android Gradle Plugin 2.2.0 ProGuard 开始保留内部类

使用 Android Studio 签署 APK 时出现 Proguard 错误

评估指令时出现 Proguard 错误

尝试在 ProGuard 中启用缩小时出现 DexGuard 错误

启用 proguard 时出现杰克逊错误(缩小)