# org.gradle.launcher.daemon.server.DaemonExpirationPeriodicCheck: Daemon will be stopped at the end of the build after running for 3 hours and 5 minutes because the build environment has reached its maximum heap size limit

- **ID:** `cicd/gradle-daemon-stopped-memory`
- **Domain:** cicd
- **Category:** resource_error
- **Error Code:** `GRADLE_OOM`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Gradle daemon runs out of heap memory due to insufficient JVM memory allocation for the build, typically when the CI runner has limited RAM or the Gradle JVM args are not tuned for large projects.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Gradle 7.6 | active | — | — |
| Gradle 8.2 | active | — | — |
| Gradle 8.5 | active | — | — |

## Workarounds

1. **Set JVM args in gradle.properties: org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m. Then restart daemon: ./gradlew --stop && ./gradlew build** (85% success)
   ```
   Set JVM args in gradle.properties: org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m. Then restart daemon: ./gradlew --stop && ./gradlew build
   ```
2. **Reduce parallel test execution: In build.gradle, set 'tasks.withType(Test).configureEach { maxParallelForks = 1 }' and 'forkEvery = 50' to limit memory per fork.** (75% success)
   ```
   Reduce parallel test execution: In build.gradle, set 'tasks.withType(Test).configureEach { maxParallelForks = 1 }' and 'forkEvery = 50' to limit memory per fork.
   ```

## Dead Ends

- **** — Increasing Gradle daemon heap via GRADLE_OPTS without checking CI runner limits often still fails because the runner's total memory is capped. (60% fail)
- **** — Disabling the daemon with --no-daemon may cause slower builds but doesn't fix OOM if the build itself is memory-hungry in the JVM process. (70% fail)
- **** — Adding more parallel forks with maxParallelForks can increase memory pressure, making OOM worse. (80% fail)
