You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Github Action中Gradle提示内存未配置但已配置的问题求助

解决GitHub Actions中Gradle内存配置不生效的问题

问题重现

在GitHub Actions执行Android单元测试时,Gradle日志提示元空间不足:

Task :app:hiltJavaCompileDebug
The Daemon will expire after the build after running out of JVM Metaspace.
The project memory settings are likely not configured or are configured to an insufficient value.
The daemon will restart for the next build, which may increase subsequent build times.
These settings can be adjusted by setting 'org.gradle.jvmargs' in 'gradle.properties'.
The currently configured max heap space is '4 GiB' and the configured max metaspace is '256 MiB'.
Daemon will be stopped at the end of the build after running out of JVM Metaspace

但本地gradle.properties已配置:

org.gradle.jvmargs=-Xmx6g -Dfile.encoding=UTF-8 -XX:+UseParallelGC -XX:MaxMetaspaceSize=256m -XX:+HeapDumpOnOutOfMemoryError

GitHub Actions配置:

- name: Run tests
  uses: gradle/gradle-build-action@v2
  with:
    cache-read-only: false
    arguments: "${{ env.command }}"

可能原因及解决方案

1. 环境变量覆盖了gradle.properties配置

GitHub Actions runner或gradle-build-action可能存在默认的Gradle JVM参数,优先级高于项目内的gradle.properties。可以通过显式设置环境变量强制覆盖:

修改GitHub Actions步骤,添加env字段传递JVM参数:

- name: Run tests
  uses: gradle/gradle-build-action@v2
  env:
    # 直接通过环境变量设置Gradle JVM参数,优先级最高
    ORG_GRADLE_PROJECT_org.gradle.jvmargs: "-Xmx6g -Dfile.encoding=UTF-8 -XX:+UseParallelGC -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError"
  with:
    cache-read-only: false
    arguments: "${{ env.command }}"

注:建议将MaxMetaspaceSize调高至512m或1g,Hilt编译对元空间需求较高,256m容易触发不足问题。

2. Gradle缓存残留旧配置

gradle-build-action的缓存可能保留了旧的内存配置,导致新配置未生效。可以通过以下方式清理缓存:

  • 在构建参数中添加--no-build-cache跳过缓存:
arguments: "${{ env.command }} --no-build-cache"
  • 或者临时设置cache-read-only: true一次,强制重新生成缓存(之后改回false)。

3. 多gradle.properties文件冲突

检查项目中是否存在多个gradle.properties文件(比如根目录和模块目录下的),模块内的配置可能覆盖根目录的设置。确保所有gradle.properties中的org.gradle.jvmargs配置一致。

内容的提问来源于stack exchange,提问作者Cheolho Jeon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:30:13