Setting Java heap space under Maven 2 on Windows

前端 未结 8 1160
情深已故
情深已故 2020-12-02 06:39

I get this message during build of my project

java.lang.OutOfMemoryError: Java heap space

How do I increase heap space, I\'ve

相关标签:
8条回答
  • 2020-12-02 07:06

    It should be the same command, except SET instead of EXPORT

    • set MAVEN_OPTS=-Xmx512m would give it 512Mb of heap
    • set MAVEN_OPTS=-Xmx2048m would give it 2Gb of heap
    0 讨论(0)
  • 2020-12-02 07:10

    On the Mac: Instead of JAVA_OPTS and MAVEN_OPTS, use _JAVA_OPTIONS instead. This works!

    0 讨论(0)
  • 2020-12-02 07:10

    You are looking for 2 options to java:

    • -Xmx maximum heap size
    • -Xms starting heap size

    Put them in your command line invocation of the java executable, like this:

    java -Xms512M -Xmx1024M my.package.MainClass
    

    Keep in mind that you may want the starting and max heap sizes to be the same, depending on the application, as it avoids resizing the heap during runtime (which can take up time in applications that need to be responsive). Resizing the heap can entail moving a lot of objects around and redoing bookkeeping.

    For every-day projects, make them whatever you think is good enough. Profile for help.

    0 讨论(0)
  • 2020-12-02 07:16

    If you are running out of heap space during the surefire (or failsafe) JUnit testing run, changing MAVEN_OPTS may not help you. I kept trying different configurations in MAVEN_OPTS with no luck until I found this post that fixed the problem.

    Basically the JUnits fork off into their own environment and ignore the settings in MAVEN_OPTS. You need to configure surefire in your pom to add more memory for the JUnits.

    Hopefully this can save someone else some time!


    Edit: Copying solution from Keith Chapman's blog just in case the link breaks some day:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <forkMode>pertest</forkMode> 
        <argLine>-Xms256m -Xmx512m</argLine>
        <testFailureIgnore>false</testFailureIgnore> 
        <skip>false</skip> 
        <includes> 
          <include>**/*IntegrationTestSuite.java</include>
        </includes>
      </configuration>
    </plugin>
    

    Update (5/31/2017): Thanks to @johnstosh for pointing this out - surefire has evolved a bit since I put this answer out there. Here is a link to their documentation and an updated code sample (arg line is still the important part for this question):

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20</version>
        <configuration>
            <forkCount>3</forkCount>
            <reuseForks>true</reuseForks>
            <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
            <systemPropertyVariables>
                <databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
            </systemPropertyVariables>
            <workingDirectory>FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
        </configuration>
      </plugin>
    
    0 讨论(0)
  • 2020-12-02 07:18

    The environment variable to set is MAVEN_OPTS, for example MAVEN_OPTS=-Xmx1024m. The maxmem configuration in the pom only applies when you set the compiler plugin to fork javac into a new JVM. Otherwise the plugin runs inside the same VM as Maven and thus within the memory passed on the command line via the MAVEN_OPTS.

    To set MAVEN_OPTS under Windows 7:

    1. Right click on My Computer and select Properties (keyboard shortcut press Windows + Pause/Break)
    2. Click the Advanced System Settings link located in the left navigation of System Properties to display the Advanced System Properties
    3. Go to the Advanced tab and click the Environment Variables button located at the bottom of the Advanced System Properties configuration window
    4. Create a New user variable, set the Variable name to MAVEN_OPTS and set the Variable value to -Xmx1024m (or more)

    Open a new command window and run mvn.

    0 讨论(0)
  • 2020-12-02 07:18

    After trying to use the MAVEN_OPTS variable with no luck, I came across this site which worked for me. So all I had to do was add -Xms128m -Xmx1024m to the default VM options and it worked.

    To change those in Eclipse, go to Window -> Preferences -> Java -> Installed JREs. Select the checked JRE/JDK and click edit.

    0 讨论(0)
提交回复
热议问题