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

Gradle构建报ListenerNotificationException内存不足问题如何解决?

Fixing Gradle Build OutOfMemoryError: Java heap space for Clean Build

Got it, this is a super common pain point with Gradle builds—especially as projects grow in size. Let's walk through the most effective fixes to get your clean build running smoothly again:

The best way to fix this long-term is to adjust Gradle's JVM heap settings in your project's gradle.properties file:

  • Navigate to your project root directory, find or create the gradle.properties file.
  • Add or update the org.gradle.jvmargs line to allocate more heap space. For example:
    org.gradle.jvmargs=-Xmx4g -Xms2g -XX:MaxMetaspaceSize=512m
    
    • Xmx4g: Sets the maximum Java heap size to 4 gigabytes (adjust this based on your system's available RAM—2g, 6g are also common values).
    • Xms2g: Sets the initial heap size to 2 gigabytes, which helps Gradle avoid frequent memory resizing.
    • XX:MaxMetaspaceSize=512m: Limits the metaspace (used for class metadata) to prevent it from eating up excess memory.

2. Temporary Fix via Command Line

If you just need to test a clean build without modifying config files, pass the heap settings directly when running the Gradle command:

  • On macOS/Linux:
    ./gradlew clean build -Dorg.gradle.jvmargs="-Xmx4g -Xms2g"
    
  • On Windows:
    gradlew.bat clean build -Dorg.gradle.jvmargs="-Xmx4g -Xms2g"
    

3. Additional Optimizations to Reduce Memory Pressure

  • Enable Gradle Daemon: Add org.gradle.daemon=true to gradle.properties. The daemon keeps a Gradle process running in the background, reusing memory and speeding up subsequent builds.
  • Upgrade Gradle: Newer Gradle versions include better memory management, incremental build improvements, and reduced overhead. Check your root build.gradle file to update the Gradle version.
  • Trim Unnecessary Tasks/Modules: If you're working on a multi-module project, build only the modules you need instead of the entire project (e.g., ./gradlew :app:clean :app:build instead of ./gradlew clean build).
  • Check for Memory-Hungry Plugins: Some third-party plugins might leak memory or consume excessive resources. Try disabling non-critical plugins temporarily to see if the issue persists.

Once you've applied these changes, run ./gradlew clean build again—you should no longer hit the OutOfMemoryError.

内容的提问来源于stack exchange,提问作者Laxmanrao Potadar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:52:48