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.

		<argLine>-Xms512m -Xmx512m</argLine>

Done =)


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.


  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


    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.


  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


    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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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