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

  • 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

    Adding MAVEN_OPTS environment variable

    Adding MAVEN_OPTS environment variable

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:

  • On your Eclipse IDE, Click on Run As… -> Run Configurations…

    Install Maven on Eclipse - Run configurations...

    Configure Maven on Eclipse – 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

    Configure Maven on Eclipse - Maven build

    Configure Maven on Eclipse – Maven build

  • And on the JRE tab, past the following text in the VM arguments section:
    Configure Maven on Eclipse - JVM arguments

    Configure Maven on Eclipse – JVM arguments

  • 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
  • maven-surefire-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.

References