Out of memory error: Java heap memory on Android Studio

后端 未结 12 790
-上瘾入骨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:15

    i'm using Windows OS. I solved this by updating _JAVA_OPTIONS to '-Xmx1024m'

    1. Win + X, choose SYSTEM
    2. Choose 'Advanced system settings'
    3. Choose 'Environment Variables'
    4. Create / Edit a variable named as '_JAVA_OPTIONS'
    5. update variable value as -Xmx1024m

    click to see snapshot

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

    The answers above are for gradle builds (or the general -Xmx). In case someone gets here for a non-gradle project, I solved the issue by increasing the maximum heap size of DEX in android studio settings => Build, Execution, Deployment => Compiler => Android Compilers

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

    This issue also may happen with usage of the debug build of Proguard. To solve it change minifyEnabled and shrinkResources to false.

    android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    
        debug {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    } }  
    
    0 讨论(0)
  • 2020-12-24 06:22

    This is old, but just add this to the build file

    dexOptions {
        javaMaxHeapSize "2g"
    }
    
    0 讨论(0)
  • 2020-12-24 06:24

    I had a similar issue on Android Studio 2.3.3. APK packaging would fail saying Java heap space when building from Android Studio and trying to process a 300MB .so library. In my case two things were needed to fix it. Your build.gradle should contain something like that:

    android {
        dexOptions {
            javaMaxHeapSize "4G"
        }
    }
    

    At the root of your project you need to have a file named gradle.properties containing the following:

    # That's needed for java to be able the create the APK with our 300MB native  library
    # Basically we grant java a 4GB heap.
    org.gradle.jvmargs=-Xmx4608m
    
    0 讨论(0)
  • 2020-12-24 06:27

    you have changed the wrong arguments, you should increase the size on -XX:MaxPermSize=512M to -XX:MaxPermSize=2048M or so :) because the android emulator uses a lot of RAM.

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