35

When running a class I have the following exception:

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded

I've tried to increase the jvmArg heap size from inside maven pom.xml of the class package:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<parent>
    (...)
</parent>


<artifactId>(...)</artifactId>
<name>(...)</name>

<properties>
    <javaOpts.Xmx>4g</javaOpts.Xmx> <!-- default that can be adjusted on the command line with -DjavaOpts.Xmx=... -->
    <(...).basedir>${project.basedir}/..</(...).basedir>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <configuration>
                <launchers>
                    <launcher>
                        <id>MyClassName</id>
                        <mainClass>(...)</mainClass>
                        <jvmArgs>
                            <jvmArg>-Xmx${javaOpts.Xmx}</jvmArg>
                        (...) 

I've tried the last cited line with many values:

  • <jvmArg>-Xmx512m{javaOpts.Xmx}</jvmArg>
  • <jvmArg>-Xmx4096M{javaOpts.Xmx}</jvmArg>
  • ...
  • <jvmArg>-Xmx10000000000M{javaOpts.Xmx}</jvmArg>

But for all of them I have the same error.

Anyone can help me? Observation: I'm running from IntelliJ IDEA.

  • 1
    Maven has nothing to do with "running". If your problem is out of memory when RUNNING you application, you need to set the heap size params at launch time, not in maven at build time. If I misunderstood please update your question – Hilikus Sep 12 '13 at 17:50
  • I assume this is a type: ```-Xmx512m{javaOpts.Xmx}``` and you actually meant ```-Xmx512m```? – Pablo Mendes Mar 05 '14 at 23:09

7 Answers7

48

Java 7

It's not a Maven issue, you need to give more memory to the VM. You do this via environment variables, and the Maven Launcher will automatically pick up these settings on load, and use them to configure the underlying Java VM.

The simple way to do it is to:

export MAVEN_OPTS="-Xmx1024M -Xss128M -XX:MaxPermSize=1024M -XX:+CMSClassUnloadingEnabled"

Windows:

set MAVEN_OPTS=-Xmx1024M -Xss128M -XX:MaxPermSize=1024M -XX:+CMSClassUnloadingEnabled

(No double quotes)

Java 8

Java 8 has replaced permanent generation with metaspace, and the new settings look like this:

export MAVEN_OPTS="-Xmx1024M -Xss128M -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=1024M -XX:+CMSClassUnloadingEnabled"

flavian
  • 28,161
  • 11
  • 65
  • 105
  • 4
    This should be added to your shell environment, not your pom.xml. – Marco Aug 08 '14 at 07:32
  • Add "set MAVEN_OPTS=-Xms1024M -Xmx2048M -XX:-UseGCOverheadLimit" line to your "mvn.bat" file as a first line of code. "mvn.bat" could be found at C:\MAVEN_INSTALLATION_DIRECTORY\bin. If you are on Unix platform, you should edit mvn.sh instead of mvn.bat. You can change -Xms and -Xmx values, based on how much memory is required by your programs to run/compile/build. – HIREN011 Mar 08 '16 at 15:28
  • FYI: the MaxPermSize option was removed in Java 8 and is now ignored. – Tony Laidig Jul 05 '17 at 14:21
  • @flavian there is mistake in your answer. I think you meant - startup memory assignment - Xms128m instead of -Xss128m - which is huuuuge stack size for each thread – devstructor Dec 14 '18 at 08:36
  • It is a maven issue in my case. Failing after 30mins when it's normally done in 3mins. – Philip Rego Jan 18 '22 at 17:00
10

I had kind of the same problem: My project wokspace consists of >250 maven modules and when opening a pom.xml in IDEA, the <project> and <parent> tags were underlined in red and this error showed up, when hovering over the broken tags:

java.lang.OutOfMemoryError: GC overhead limit exceeded

My solution:

  • set high values in >Settings >Build, Execution, Deployment >Build Tools >Maven >Importing - e.g. -Xmx1g and
  • change the maven implementation under >Settings >Build, Execution, Deployment >Build Tools >Maven (Maven home directory) from (Bundled) Maven 3 to my local maven installation (not the one bundled with IDEA), e.g. to /opt/maven/apache-maven-3.3.9: Maven Settings Screenshot

Since I had problems with the bundled maven before, I just don't trust the bundled maven.

Alexander
  • 2,925
  • 3
  • 33
  • 36
  • 1
    Adding a heap size option on the Maven:Importing screen (but keeping the bundled Maven 3) worked for me when trying to import the entire wildfly-swarm project in IntelliJ. – Frans Apr 30 '18 at 06:45
7

Adding parallel garbage collection to the top answer above is probably the best option:

-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC
2

In some cases you may disable GC limit:

-XX:-UseGCOverheadLimit
Nami
  • 1,215
  • 11
  • 21
2

Sorry for very late response, but I think my answer might help other people as well.

scala-maven-plugin doesn't work with MAVEN_OPTS, the only way to increase memory is to use jvmArgs in the plugin configuration. But you should put jvmArgs not inside launcher section, but inside configuration section, like this:

<configuration>
    <jvmArgs>
        <jvmArg>-Xmx1024m</jvmArg>
        <jvmArg>-Xms256m</jvmArg>
    </jvmArgs>
</configuration>

This works for me.

sap1ens
  • 2,877
  • 1
  • 27
  • 30
0

You are adding jvmArgs to the maven-scala-plugin config in your pom.xml.

What do you mean you are running from IntelliJ IDEA? How exactly are you doing that? Via their build/run you are not going via Maven at all! Then changing the maven-scala-plugin launchers in your pom.xml is basically useless. To change the run configurations for IntelliJ see this answer.

If you really want to run from Maven, after you add the jvmArg params to the pom.xml, you then need to run the plugin:

mvm scala:run -Dlauncher=MyClassName

Note: where you put MyClassName, that is actually the name of the launcher, not the class. For many projects, people pick reasonable names for the launchers and that makes them often coincide with the local name of the classes. But you could call it MyLauncher if you wanted.

Reading maven-scala-plugin's docs might help you.

Community
  • 1
  • 1
Pablo Mendes
  • 391
  • 1
  • 8
0

None of the answers above worked for me. What did work was this answer from Intellij support forum. It suggests increasing build process heap size to 1024 MB by going to Build, Execution, Deployment > Compiler > Build Process heap size(Mbytes)

mindreader
  • 1,763
  • 1
  • 20
  • 35