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

﹏ヽ暗。殇╰゛Y 2021-09-24 01:18 508阅读 0赞

1 问题

glide加载图片出现oom

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

2 解决办法

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

  1. android:largeHeap="true"
  2. <application
  3. android:allowBackup="true"
  4. android:icon="@mipmap/guruji"
  5. android:label="@string/app_name"
  6. android:supportsRtl="true"
  7. android:largeHeap="true"
  8. android:theme="@style/AppTheme">

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

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

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

发表评论

表情:
评论列表 (有 0 条评论,508人围观)

还没有评论,来说两句吧...

相关阅读