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:
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.
LikeLike
Good to know that i could help. =)
LikeLike
Thanks for the clarification and the pom sample.
LikeLike
You are welcome and thanks for your comment. =D
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
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
LikeLike
An oldie but a goodie, this just saved ME. Two thumbs up!
LikeLike