42

I am using android studio 1.0 RC for 64 bit linux.

When I run my application I am getting

"java.lang.OutOfMemoryError: GC overhead limit exceeded"

When I searched on how to solve this error I got solutions like add:

  • -XX:-UseGCOverheadLimit to studio.vmoptions or studio64.vmoptions
  • -Xmx2000m to studio.vmoptions or studio64.vmoptions etc.

These did not work for me.

Please help. Thanks in advance

2
  • I solved this issue by adding dexOptions { incremental true javaMaxHeapSize "4g" } to the android closure in build.gradle file. Found this answer in groups.google.com/forum/#!topic/adt-dev/r4p-sBLl7DQ Commented Nov 27, 2014 at 6:53
  • It's probably due to an IDE plugin, or something in gradle tasks maybe. I had this problem when I was using an older version of CodeGlance (now fixed) Commented Jan 18, 2016 at 15:33

8 Answers 8

61

I solved this issue by adding

dexOptions { 
          incremental true 
          javaMaxHeapSize "4g" 
} 

to the android closure in build.gradle file. Found this answer in

OutOfMemoryError: GC overhead limit exceeded

Sign up to request clarification or add additional context in comments.

Comments

39

Add this to your "gradle.properties" file:

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError

Also, read this article. You might be able to make the building a bit faster, by adding a combination of those:

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true

EDIT: an updated answer based on my experience:

On Android Studio, choose Help -> Edit custom VM options , and then set the max memory the IDE is allowed to use. For example, if you want 5GB, use this:

-Xmx5g

Save the file, close all windows of the IDE (make sure it has no processes) and then restart the IDE.


EDIT: Android Studio now has RAM configuration in its settings. Go to "help"->"change memory settings".

8 Comments

@androiddeveloper It happened again after 3 builds. IDE using 1.3 gb and openjdk is using 0.5gb I gave them both 4GBs max.
@stevemoretz Wow. Try to update the IDE to latest version. Maybe even try beta or canary versions... I remember it had serious memory issues in the past, but I think it got better... I also think that opening the layout designer or its preview can consume some memory.
@androiddeveloper I'm on 3.2.1 it's not old.It could be one of this stupid free github libraries my colleges added to the project trying to remove and test each one by one.I'll let you know the results maybe can help you update your answer or something.I pretty much think it's an external library my code can't create such error because it's working with limited data.And even those are controlled.
This library com.theartofdev.edmodo:android-image-cropper with great documentation and amazingly awesome content was causing the error!!I can't seriously believe this.So please check your external libraries also when you get this error!Don't trust even the high quality ones.
@androiddeveloper I removed it from my gradle and I have no problem now.Do you use androidX?I used the latest version by this day with androidX and pretty much sure that was the problem.Not even a single error after getting rid of that!
|
5

Find the Memory Settings

  1. Cmd + Shift + A on Mac (Ctrl + Shift + A on Windows) or click on Help
  2. Type "Memory Settings"

under Preferences/ Settings and increase the IDE Heap Size and/ or the Daemon Heap Size to your satisfaction enter image description here

Comments

4

I had the same issue too - mine was for a different reason. I was working on backing up some files and accidentally dropped a big file in the resource folder. It was close to 40MB.

Once this file was removed, the error was gone.

Comments

4

Add this line in your build.gradle

dexOptions
         {
               incremental false
               javaMaxHeapSize "2048M" 
               preDexLibraries = false
         }

1 Comment

What does preDexLibraries do?
3

I also have this problem my solution is : Just modify the gradle.properties in the root of your project :

org.gradle.jvmargs=-XX\:MaxHeapSize\=512m -Xmx512m

the default jvmargs is 256 when you compile a lot of channel apk then will cause this problem !

Comments

1

For Android projects you use gradle plugin, at root build.gradle.kts:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:${gradle_plugin_version}")
    }
}

Upgrade gradle_plugin_version to the latest one.

The reason for that is the latest Gradle version should be used with the latest Gradle plugin version.

For example, for Gradle 6.3 you should use gradle-plugin 4.0.1:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.0.1")
    }
}

Comments

0

If you are using IntelllijIdea, From the main menu, select Help | Change Memory Settings.

Set the necessary amount of memory that you want to allocate and click Save and Restart.

This action changes the value of the -Xmx option used by the JVM and restarts IntelliJ IDEA with the new setting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.