无法分配具有 16777120 个可用字节和 40MB 的 105949236 字节分配,直到 OOM

Failed to allocate a 105949236 byte allocation with 16777120 free bytes and 40MB until OOM

当我尝试在我的 android 应用程序中移动到地图 activity 时,出现以下错误。我已经尝试了很多不同的方法来通过阅读其他类似的问题来修复它,但我无法修复它。如果有人可以帮忙,谢谢。

这是我收到的日志文件中的错误。

FATAL EXCEPTION: main
Process: com.example.neil.studicom, PID: 30587
java.lang.OutOfMemoryError: Failed to allocate a 105949236 byte allocation with 16777120 free bytes and 40MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:726)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:547)
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:575)
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:605)
    at com.example.neil.studicom.MapsActivity.resizeMapIcons(MapsActivity.java:42)
    at com.example.neil.studicom.MapsActivity.onMapReady(MapsActivity.java:53)
    at com.google.android.gms.maps.SupportMapFragment$zza.zza(Unknown Source)
    at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
    at android.os.Binder.transact(Binder.java:380)
    at com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82)
    at maps.ei.bu.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

Gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

     /* dexOptions {
        javaMaxHeapSize "4g"
    }*/

    defaultConfig {
        applicationId "com.example.neil.studicom"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    /*compile 'org.apache.httpcomponents:httpmime:4.5.1'*/
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.microsoft.azure:azure-mobile-services-android-sdk:2.0.3'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:design:23.2.1'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    compile 'com.google.code.gson:gson:2.3'
}

删除这个

 compile 'com.google.android.gms:play-services:8.4.0'

并添加

compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'

在你的 gradle

dexOptions {
    //incremental true
    javaMaxHeapSize "2048M"
}

这是因为服务包含项目可能不需要的所有库。所以请使用您真正需要的库

另请参阅this

您的 resizeMapIcons() 方法正在尝试使用 decodeResource(),这会触发 105949236 字节的分配。那是〜100MB。它相当于 5146 像素 x 5146 像素的图像。没有Androidscreen是那个尺寸;你当然不需要那么大的图标

您需要找到此资源并大幅降低其分辨率。