Explain Codes LogoExplain Codes Logo

"java.lang.outofmemoryerror: PermGen space" in Maven build

java
memory-management
maven-configuration
jvm-optimization
Nikita BarsukovbyNikita Barsukov·Feb 26, 2025
TLDR

Resolve java.lang.OutOfMemoryError: PermGen space by beefing up your Maven's PermGen size with:

export MAVEN_OPTS="-XX:MaxPermSize=256m"

These settings allocate a hefty 256MB to PermGen, ensuring those class metadata don't waste away during your build.

Effective memory tweaking

The JVM's initial (-Xms) and max heap size (-Xmx) could use a few tweaks to better cater for your project's demands. A well-rounded JVM memory setting could be:

export MAVEN_OPTS="-Xms512m -Xmx2048m -XX:MaxPermSize=512m" # Loads of memory now, feels like a memory palace!

Plugin memory configuration

You can specify memory limits for maven-compiler and maven-surefire plugins directly in the pom.xml file:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <fork>true</fork> <meminitial>256m</meminitial> <maxmem>512m</maxmem> <!-- Take that, memory issues! --> </configuration> </plugin>

Carry on with similar adjustments in your maven-surefire-plugin to give tests a memory boost:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <forkCount>1C</forkCount> <argLine>-XX:MaxPermSize=256m</argLine> <!-- More memory for the win! --> </configuration> </plugin>

The Maven ecosystem and PermGen space

Within Maven's ecosystem class loading could be the nemesis causing OutOfMemoryError: PermGen space. Coming to terms with Maven's class loading process can help manage the size of PermGen area. Remember, with great power comes great responsibility to precisely manage dependencies and plugins.

Configuring MAVEN_OPTS in Windows

On a Windows environment MAVEN_OPTS need to be declared as:

set MAVEN_OPTS=-XX:MaxPermSize=256m # 'Expand the parking lot,' said one Windows user to another.

Don't forget to give your console a quick reboot after this to infuse life into the new environment variables before brewing your next Maven build.

Learning from the community

Stay thirsty for wisdom from ever-evolving community discussions. Platforms like StackOverflow are a treasure trove of insights and latest tweaks. Just like coffee fuels developers, plugin updates fuel efficient Maven operations, so keep 'em coming!