Question
How can I fix the 'java.lang.OutOfMemoryError: PermGen space' error when building a Maven project?
Answer
The 'java.lang.OutOfMemoryError: PermGen space' error typically occurs when the Java Virtual Machine (JVM) runs out of memory reserved for the Permanent Generation. This section of memory stores metadata required for classes and methods. When this memory is exhausted, Maven will fail to build your project. Below are detailed steps to resolve this issue within the Maven ecosystem.
// Bash command to set MAVEN_OPTS with increased PermGen space
export MAVEN_OPTS='-Xmx1024m -XX:MaxPermSize=512m' // Increase memory allocation for heap and PermGen.
// If using Java 8 or later
export MAVEN_OPTS='-Xmx1024m -XX:MaxMetaspaceSize=512m' // Use Metaspace settings instead.
Causes
- Excessive classloading due to a large number of plugins or dependencies in the Maven build.
- Large project size leading to too many classes being loaded concurrently.
- Lack of proper memory settings for the JVM.
Solutions
- Increase the PermGen space setting by modifying the MAVEN_OPTS environment variable to include `-XX:MaxPermSize=256m` or higher. For example: `export MAVEN_OPTS='-XX:MaxPermSize=512m'`
- If you're using a recent version of Maven and JDK, consider switching to using `-XX:MaxMetaspaceSize` instead, as PermGen has been deprecated.
Common Mistakes
Mistake: Not restarting the terminal or IDE after changing MAVEN_OPTS.
Solution: Always restart your terminal or IDE to apply the new environment settings.
Mistake: Setting the memory limits too high can lead to other performance issues.
Solution: Monitor memory usage after making adjustments and find a suitable balance.
Helpers
- java.lang.OutOfMemoryError
- OutOfMemoryError PermGen
- Maven build error
- increase PermGen space Maven
- fix PermGen space error