分享

Android之java.lang.OutOfMemoryError: Failed to allocate a ** byte allocation with **free bytes and 2M

 陈喻 2021-10-19

1 问题

glide加载图片出现oom

java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

2 解决办法

1) 简单粗暴点的在AndroidManifest.xml添加如下,增大安卓虚拟机内存

android:largeHeap="true"
<application
    android:allowBackup="true"
    android:icon="@mipmap/guruji"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    android:theme="@style/AppTheme">

2)在glide里面设置禁止内存缓存和使用磁盘缓存

public static final RequestOptions requestOptions = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).skipMemoryCache(true);

然后使用asDrawable代替asBitmap,drawable更省内存。 

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多