# java.lang.OutOfMemoryError：Java 堆空间

- **ID:** `cicd/jenkins-maven-compile-out-of-memory`
- **领域:** cicd
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

Jenkins 中的 Maven 构建耗尽了 Java 堆空间，通常是由于 JVM 内存分配不足或构建插件中存在内存泄漏。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Jenkins 2.440 | active | — | — |
| Maven 3.9.6 | active | — | — |
| OpenJDK 17 | active | — | — |

## 解决方案

1. ```
   Increase Maven's JVM heap via MAVEN_OPTS: `export MAVEN_OPTS="-Xmx2g -Xms512m"` before the build step, or configure in Jenkins pipeline: `withEnv(['MAVEN_OPTS=-Xmx2g']) { sh 'mvn clean install' }`.
   ```
2. ```
   Add `<forkCount>1</forkCount>` and `<argLine>-Xmx1g</argLine>` to the Maven Surefire plugin configuration in pom.xml to limit test forking memory.
   ```

## 无效尝试

- **** — The Maven build runs as a separate process on the agent, not on the master; increasing master heap does not affect the build agent's JVM. (95% 失败率)
- **** — Disabling necessary plugins breaks the build; memory issues are usually due to the compiler or surefire plugin handling large codebases, not plugin count. (70% 失败率)
