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:
1. Persistently Increase Gradle Heap Memory (Recommended)
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.propertiesfile. - Add or update the
org.gradle.jvmargsline to allocate more heap space. For example:org.gradle.jvmargs=-Xmx4g -Xms2g -XX:MaxMetaspaceSize=512mXmx4g: 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=truetogradle.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.gradlefile 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:buildinstead 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
相关产品推荐
相关产品推荐

