Maven – Set Java Heap Memory for JUnit in maven-surefire-plugin

Previously i have talked about setting Java Heap Memory in Maven.

But i find that the OutOfMemoryError still exists when running the JUnit test during build. =.=

After searching in Google, i realize that in Maven
JUnit tests ignore the environment variable MAVEN_OPTS and the JVM settings in Run Configurations.

So if u have OutOfMemoryError when running unit test in Maven, you have to set the Java Heap Memory in the maven-surefire-plugin inside the pom.xml.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.5</version>
	<configuration>
		<argLine>-Xms512m -Xmx512m</argLine>
	</configuration>
</plugin>

Done =)

Reference:

10 thoughts on “Maven – Set Java Heap Memory for JUnit in maven-surefire-plugin”

  1. Thanks for pointing out this option!
    I’ve had to profile the memory consumption with jconsole of my mvn test build to realize that JUnit isn’t picking up its memory settings from MAVEN_OPTS.

    Like

  2. I was looking for some workarround to override surefire’s memory config from Jenkins… i cant believe someone thought it was a good idea to let developers configure it… U_U

    Like

    1. Have you tried to set the environment variables by passing the arguments thru Jenkins? So in that case you don’t need to hardcode it in the pom.xml.

      Like

  3. this applies at the plugin level that means to all the test classes in that plugin. but is there any way of applying this only to a perticular junit class in that plugin . Thanks in advance

    Like

    1. The heap size is a JVM attribute and should be pass to the JVM before running test cases. If you really want to have different heap size for different test cases, i guess you need to re initialize the JVM.

      One simple workaround is to make the argLine a variable and pass the value explicitly on command line.
      surefire:test – argLine

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.