Search code examples
mavenspring-bootout-of-memorymaven-surefire-pluginpermgen

Running Spring Boot "context loads" test in maven causes OutOfMemoryError


I have a multi-module Maven project with two modules being Spring Boot applications. Each of them has a simple test that the Spring application context loads successfully (my tests are very similar to this one). I run this tests with the following command in project root:

mvn -P IntegrationTests clean test

During context initialization things go out of my control, the application "eats" memory (heap size grows quickly to 4 gigabytes) and then the context fails to start with java.lang.OutOfMemoryError: PermGen space error (yes, I run it in Java 7).

Monitoring task manager during testing I noticed that maven spawns two new processes that have something to do with surefire plugin. I have no idea where it comes from, because I don't add the surefire plugin in my pom.xml.

high load

Previously when encountered the same error somewhere I specified VM options (-Xmx256m -Xms128m -XX:MaxPermSize=256m -XX:PermSize=128m for example) and the problem was solved.

This time I tried to

  1. set MAVEN_OPTS environment variable
  2. set VM options (when running mvn test in IntelliJ IDEA) - it affected main java process but not its children
  3. add -Drun.jvmArguments="..." in command line

but the problem persists.

Please help me to fight the OutOfMemoryError in tests.


Solution

  • Add Surefire plugin explicitly to module-specific pom.xml and configure VM options there. I like this solution because this way VM options are

    • passed to the spawned surefire processes (which should solve your problem)
    • affect only test application builds
    • shared between developers in your team
    • configurable independently for every module
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Xmx256m -Xms128m -XX:MaxPermSize=256m -XX:PermSize=128m</argLine>
                </configuration>
            </plugin>
    
            <!-- your other plugins go here -->
        </plugins>
    </build>