致命异常:Thread-18 java.lang.OutOfMemoryError:在android volley中使用较低版本的设备

时间:2022-10-31 21:11:00

I am getting frequently OOM with volley library in lower versions of android devices.

我经常在较低版本的Android设备中使用排球库进行OOM。

I am using BitmapLruCache

我正在使用BitmapLruCache

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {

public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public BitmapLruCache() {
    this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

}

}

Your suggestions will be appreciated. Thank You.

您的建议将不胜感激。谢谢。

03-11 12:24:02.768: E/AndroidRuntime(18454): FATAL EXCEPTION: Thread-18
03-11 12:24:02.768: E/AndroidRuntime(18454): java.lang.OutOfMemoryError: (Heap Size=19463KB, Allocated=14480KB, Bitmap Size=10107KB)
03-11 12:24:02.768: E/AndroidRuntime(18454):    at com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101)
03-11 12:24:02.768: E/AndroidRuntime(18454):    at com.android.volley.toolbox.PoolingByteArrayOutputStream.<init>(PoolingByteArrayOutputStream.java:53)
03-11 12:24:02.768: E/AndroidRuntime(18454):    at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:203)
03-11 12:24:02.768: E/AndroidRuntime(18454):    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104)
03-11 12:24:02.768: E/AndroidRuntime(18454):    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)

2 个解决方案

#1


3  

For low memory devices -

对于低内存设备 -

LRU Bitmap cache takes too much memory for low-memory devices.

LRU位图缓存为低内存设备占用过多内存。

Check Explanation - bug 50733: Fix OOM error on low-memory devices.

检查说明 - 错误50733:修复低内存设备上的OOM错误。

#2


2  

It was getting Out Of Memory exception when I was trying to download 10 images with size 2 MB.

当我试图下载10个大小为2 MB的图像时,它出现了Out Of Memory异常。

As volley itself says that It can not download or upload large amount of data.

正如凌空本身所说,它无法下载或上传大量数据。

Here, I fixed OOM by re-sizing images to lower size as possible as i want in server.

在这里,我通过在服务器中根据需要将图像重新调整为较小的大小来修复OOM。

Example: Actually I need images with sizes 400x200 but It was with sizes 1500x1600 in server, so I made images with sizes 400x200 in server and my list view is easily downloading images from server.

示例:实际上我需要大小为400x200的图像,但服务器中的大小为1500x1600,因此我在服务器中制作了大小为400x200的图像,我的列表视图很容易从服务器下载图像。

#1


3  

For low memory devices -

对于低内存设备 -

LRU Bitmap cache takes too much memory for low-memory devices.

LRU位图缓存为低内存设备占用过多内存。

Check Explanation - bug 50733: Fix OOM error on low-memory devices.

检查说明 - 错误50733:修复低内存设备上的OOM错误。

#2


2  

It was getting Out Of Memory exception when I was trying to download 10 images with size 2 MB.

当我试图下载10个大小为2 MB的图像时,它出现了Out Of Memory异常。

As volley itself says that It can not download or upload large amount of data.

正如凌空本身所说,它无法下载或上传大量数据。

Here, I fixed OOM by re-sizing images to lower size as possible as i want in server.

在这里,我通过在服务器中根据需要将图像重新调整为较小的大小来修复OOM。

Example: Actually I need images with sizes 400x200 but It was with sizes 1500x1600 in server, so I made images with sizes 400x200 in server and my list view is easily downloading images from server.

示例:实际上我需要大小为400x200的图像,但服务器中的大小为1500x1600,因此我在服务器中制作了大小为400x200的图像,我的列表视图很容易从服务器下载图像。