instant run java.lang.OutOfMemoryError: GC overhead limit exceeded
Asked Answered
R

4

15

I have upgraded to Android Studio 2.1 and I got this error while I am trying to build & run my corporate big project:

Execution failed for task ':app:transformClassesWithDexForMyAppDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded

I have searched through the forum and disabled instant run, also write to my build.gradle:

dexOptions {
    incremental true
    javaMaxHeapSize "6g"
}
...
dependencies{
    compile 'com.android.support:multidex:'
}

But it doasn't solved my problem. I have multidex enabled in my gradle, because I get the error without it:

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

so this was the solution for it, and it worked before with the previous versions of Android Studio (also working for the others in the company, who are using Android Studio 1.4-2.0) but not for me since I upgraded my Android Studio.

Do anyone have an idea what can cause the problem?

What is also interesting that if I just make the project I don't get the error, only if I try to Run it. Any idea is appreciated!

EDIT 1:

What is also interesting, that if I restart my android studio, than the first run is successful, but the second is not.

EDIT 2:

If I set the heap size to bigger (like 8-10g) than the app not even compiles at first run.

EDIT 3:

It seems that the problem is with instant run, if I force android studio to not use it (like deploying to two devices at once or changeing gradle.properties like in the answer) the error disappears.

Reversion answered 15/4, 2016 at 9:18 Comment(8)
Instant run adds methods to the program and apparently that makes you go over the 65k limit. Are you by chance adding the play services to your dependencies?Komsomolsk
I have disabled instant run in the settings like this answer: https://mcmap.net/q/94247/-android-studio-google-jar-file-causing-gc-overhead-limit-exceeded-errorReversion
I've been all day trying to resolve this error ...Tumefy
Any suggetions to solve it? @ReversionTumefy
If I would have I wouldn't ask :,˙(Reversion
@Komsomolsk any other idea?Reversion
@Reversion check my answer, I solved.Tumefy
Let us continue this discussion in chat.Reversion
T
31

Add this to your gradle.properties file.

# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
org.gradle.daemon=true

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true

# Enables new incubating mode that makes Gradle selective when configuring projects.
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true

Found Here

And on my build.gradle :

....
     dexOptions
         {
               incremental false
               javaMaxHeapSize "2048M" 
               preDexLibraries = false
         }//end dexOptions

....
Tumefy answered 15/4, 2016 at 10:32 Comment(8)
It seems that the problem is with the instant run. This settings is disables it as far as I understand.Reversion
@Reversion I read it but not with my problem, because I was Instant run disabledTumefy
So you have the instant run feature after you edited your gradle.properties? You have the lighting icon next to your run icon? because I enabled instant run in settings but I don't have it. :/Reversion
Yes, I have enabled now with my edited gradle.properties file , and yes, when I run the app, the lighting icon appears automatically on my run icon... maybe your Android Studio it's corrupt?Tumefy
Ran into this issue today as well, these changes fixed it thanks!Lactometer
What about if I don't have gradle.properies?Pape
Are you on AndroidStudio? @PapeTumefy
Hi, from what i've read (developer.android.com/studio/run/index.html), org.gradle.jvmargs should be -Xmx3072m which composed of default value -Xmx10248m + javaMaxHeapSize "2048M", but why you only set it to -Xmx2048m ?Nidifugous
E
4

Step1: Change build.grade

defaultConfig {
        ...
        // Enabling multidex support.
        multiDexEnabled true
       }

dependencies {
        ...
        compile 'com.android.support:multidex:1.0.0'
    }

Step2: Setting on the Application class

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    MultiDex.install(this);
}

}

Step3: Change grade.properties

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

It will work!. Thanks.

Erythrism answered 20/7, 2016 at 6:58 Comment(0)
A
0

Another method to set the heap size for particular jobs is to use environment variables for each job. This ensures that the memory is available when the job that requires a higher memory is not in use.

GRADLE_OPTS="-Dorg.gradle.jvmargs=-Xms1024M -Xmx8192M -XX:PermSize=512M -XX:MaxPermSize=2048 -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

JAVA_OPTS="-XX:MaxPermSize=2048M"
Atrocious answered 15/4, 2020 at 6:19 Comment(0)
P
-2
defaultConfig {
    multiDexEnabled =true
}

add this to your build.gradle file of the app

Pongid answered 30/8, 2016 at 4:45 Comment(2)
explain the answer, so that other can understand it, not only question guy asked quwstion.Selfeducated
I had already enabled multidex, as I wrote in my question "I have multidex enabled in my gradle"...Reversion

© 2022 - 2024 — McMap. All rights reserved.