Roufid

3 ways to solve java.lang.OutOfMemoryError: PermGen space in Maven build

Maven

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:

  1. Adding the environment variable MAVEN_OPTS
  2. Fix the problem on Eclipse
  3. 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

If you are using command line you can set the variable before running the build as following:

On Unix

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:

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:

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.

References