Table of contents
Problem
You may face the below error while running a Maven build
java.lang.OutOfMemoryError: PermGen space
Solution
You must increase the Permanent Generation (PermGen) memory size of your JVM by passing the argument -XX:MaxPermSize and below how to perform it according to how you are executing the Maven build:
- Adding the environment variable MAVEN_OPTS
- Fix the problem on Eclipse
- Fix the problem in the pom.xml
1- Adding the environment variable MAVEN_OPTS
The simplest way to solve this problem is by adding the environment variable MAVEN_OPTS in which you will specify the PermGen size.
MAVEN_OPTS is used to pass parameters to the Java VM when running Maven.
On Windows
- Go to System properties -> Advanced system settings -> Advanced -> environment variable
- On the System variables section click on New…
- Fill the form with the following
Variable name: MAVEN_OPTS
Variable value: -XX:MaxPermSize=128m
If you are using command line you can set the variable before running the build as following:
1 |
set MAVEN_OPTS=-XX:MaxPermSize=128m |
On Unix
1 |
export MAVEN_OPTS="-XX:MaxPermSize=128m" |
In this example the JVM will allocate 128 MegaBytes for the PermGen. You can fit this value according to your need.
2- Running Maven in Eclipse
If you are running Maven from your IDE you must create a new Run configuration with the necessary JVM Arguments. Below how to do:
- On your Eclipse IDE, Click on Run As… -> Run Configurations…
- Double-click on Maven Build to create a new Maven Build
- Give a name to your Maven build to identify it
- On the base directory, type ${project_loc} to execute the build on the selected project
- On Goals, type the Maven goals you want to execute. For example clean install
- And on the JRE tab, past the following text in the VM arguments section:
1-XX:MaxPermSize=128m - Click Apply to save the build configuration
- Click Run to run the build on your project
3- Adding the configuration in the pom.xml
Sometimes it’s very useful to create portable Maven builds that can be executed in different environments. The idea is to specify the PermGen size in the pom.xml of your project and this depends on whether or not the Plugin you are using allow it or not. Below some Maven Plugins allowing you to pass arguments to the JVM:
- maven-compiler-plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <fork>true</fork> <meminitial>128m</meminitial> <maxmem>512m</maxmem> <compilerArgs> <arg>-XX:MaxPermSize=128m</arg> </compilerArgs> </configuration> </plugin> |
- maven-surefire-plugin
1 2 3 4 5 6 7 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>-XX:MaxPermSize=128m</argLine> </configuration> </plugin> |
What happened ?
Actually it’s not a Maven problem but an insufficient memory allocation for PermGen at your JVM. PermGen is where the internal representations of java classes and some other information are stored. During the Maven build you want to execute the Maven classLoaders are storing the necessary classes information in the JVM.
The error java.lang.OutOfMemoryError: PermGen space is thrown when a class information must be stored by no space is available.