Out of memory error: Java heap memory on Android Studio

后端 未结 12 789
-上瘾入骨i
-上瘾入骨i 2020-12-24 05:47

How do I fix java.lang.OutOfMemoryError: Java heap space when compiling my Android project?

I get this after I upgraded to version 1 of Android Studio.

相关标签:
12条回答
  • 2020-12-24 06:00

    Just add this to the gradle file:

    android{
    testOptions {
            ....
            unitTests.all {
                jvmArgs "-Xmx2g" // to avoid outOfMemory exception
            }
            ....
        }
    }
    
    0 讨论(0)
  • 2020-12-24 06:08

    None of the answers worked for me but finally I found something that worked in my case:

    Automatically managing paging files size

    In windows:

    1. Under "Advanced System Settings"
    2. Advanced Tab
    3. Performance section, settings...
    4. Advanced Tab
    5. Virtual memory... Change...
    6. Select Automatically manage paging files size for all drives

    I don't remember why I had this unchecked but this was causing trouble in my gradle builds (with flutter).

    0 讨论(0)
  • 2020-12-24 06:11

    Here is what I did:

    1st step: In your App, go to GRADLE SCRIPTS -> GRADLE.PROPERTIES -> change org.gradle.jvmargs=-Xmxm to org.gradle.jvmargs=-Xmx4G

    2nd Step: In your App, go to GRADLE SCRIPTS -> build.gradle -> in the android{

    //add this line:

    dexOptions {
       javaMaxHeapSize = "4G"
       }
    }
    
    0 讨论(0)
  • 2020-12-24 06:12

    Build => Clean Project

    then

    Build => Rebuild Project

    When I do this, it always fixes for me.

    0 讨论(0)
  • 2020-12-24 06:13

    I've finally solved it by limiting the Google Play service in gradle dependency.

    The problem however was caused by the Google Play service, and by simply change it to a specific library, it is now solved.

     com.google.android.gms:play-services-base:6.5.87
    

    UPDATE

    More info on android guides

    0 讨论(0)
  • 2020-12-24 06:13

    assign more memory to Gradle in the project's gradle.properties file.For example:

    org.gradle.jvmargs=-Xmx1024m

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