32

I was wondering if it is possible to specify Maven memory boundaries with a syntax similar to:

mvn -Dtest=FooTest -DXmx=512M clean test

I tried a couple of variations till now, unsuccessfully.
I am aware of MAVEN_OPTS environment variable, but I would like to avoid that.

Related to the above question, it would be nice to know if there is the possibility to specify the memory behavior of the surefire plugin in a similar manner, so that it forks the jvm using the overridden memory amount (eventually overriding the <argLine> parameter in the pom plugin configuration, if present)

Moe Far
  • 2,742
  • 2
  • 23
  • 41
Ghiro
  • 500
  • 1
  • 4
  • 11

3 Answers3

78

To specify the max memory (not via MAVEN_OPTS as originally requested) you can do the following:

mvn clean install -DargLine="-Xmx1524m"
Chris Ritchie
  • 4,749
  • 2
  • 36
  • 33
  • 5
    This is actually not true. If you use a [jvisualvm](https://visualvm.java.net/), that you even already have in your JDK (under bin directory), you will see that the argument is not applied on the JVM, but as a system property argLine=-Xmx1524m . – youreyecheek Dec 22 '15 at 11:01
  • 3
    This is not true, `argLine` only affects a forked process created for running tests (Surefire), not the actual process used to do compilation etc. Whereas `MAVEN_OPTS` affects everything. – Adam Burley Feb 26 '18 at 19:21
13

You can configure the surefire-plugin to use more memory. Take a look on Strange Maven out of memory error.

Update: If you would like to set the parameter from command-line take a look on {{mvn.bat}} (or {{mvn}} shell script) in your maven installation directory. It uses with additional options specified in command line.

Next possibility is to set surefire-plugin and use properties specified in command-line, e.g. mvn ... -Dram=512

and

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <forkMode>once</forkMode>
        <argLine>-Xms${ram}m -Xmx${ram}m</argLine>
    </configuration>
</plugin>
Community
  • 1
  • 1
amra
  • 16,125
  • 7
  • 50
  • 47
  • 1
    hello amra, thanks for your reply. As mentioned in my post, I am interested in specifying memory setting as arguments to the mvn command. Both for mvn itself, or i.e. the surfire plugin, if possible. Cheers – Ghiro Sep 23 '11 at 12:30
4

Since Maven 3.3.1 a default can also be specified in ${maven.projectBasedir}/.mvn/jvm.config

It is documented here: https://maven.apache.org/docs/3.3.1/release-notes.html#JVM_and_Command_Line_Options

Kees van Dieren
  • 1,232
  • 11
  • 15