GridView内存不足通用图像加载器

时间:2014-06-22 18:40:12

标签: android gridview bitmap out-of-memory universal-image-loader

我的许多用户(主要使用三星设备)在使用gridview的应用程序时出现Out of Memory错误。请注意,我无法在我的任何设备(Nexus)上重现此内容

根据Universal Image Loader文档:

If you often got OutOfMemoryError in your app using Universal Image Loader then:

Disable caching in memory. If OOM is still occurs then it's a defect of your app. 

由于我已经尝试过禁用cacheInMemory,显然我在应用程序中有一个缺陷但是我无法看到问题所在。网格一次最多显示12个项目(整个列表中大约1400个)。我没有设置任何ImageLoaderConfiguration设置,也许我应该是?

private ArrayList<Theme> mThemes;
private static DisplayImageOptions options;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mRootView = inflater.inflate(R.layout.ac_image_grid, container, false);
    options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error)
            .cacheInMemory(false)
            .cacheOnDisc(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
    }
    public class ImageAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        int result = 0;
        if (mThemes != null) {
            result = mThemes.size();
        }
        return result;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;

        if (convertView == null) {
            imageView = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }

        Theme theme = mThemes.get(position);

        imageLoader.displayImage(theme.getImageURL(), imageView, options); ***ERROR occurs here

        return imageView;
    }
}

ac_image_grid看起来像:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_relative"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ptr_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<GridView 
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:horizontalSpacing="4dip"
    android:numColumns="auto_fit"
    android:columnWidth="150dip"
    android:stretchMode="columnWidth"
    android:verticalSpacing="4dip"
    android:padding="4dip" />
    </uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
</RelativeLayout>

item_grid_image:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:contentDescription="ImgDesc"
    android:scaleType="centerCrop" />

编辑:模拟器的转储(仍然无法重现错误)并且有以下内存泄漏嫌疑人(我不知道他们的意思)。

 One instance of "android.widget.GridView" loaded by "<system class loader>" occupies 8,656,352 (15.66%) bytes. The memory is accumulated in one instance of "android.widget.GridView" loaded by "<system class loader>".

Keywords
android.widget.GridView

并且

One instance of "android.widget.GridView" loaded by "<system class loader>" occupies 7,214,272 (13.05%) bytes. The memory is accumulated in one instance of "android.view.View[]" loaded by "<system class loader>".

Keywords
android.view.View[]
android.widget.GridView

购买了其中一个受影响的设备,这是日志:

06-28 00:15:28.797    5204-5204/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 12.665MB for 1440016-byte allocation
06-28 00:15:28.857    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 861K, 13% free 10350K/11783K, paused 2ms+2ms
06-28 00:15:28.937    5204-5228/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 13.869MB for 720016-byte allocation
06-28 00:15:28.987    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 186K, 5% free 12653K/13255K, paused 2ms+3ms
06-28 00:15:29.037    5204-5230/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 17.556MB for 720016-byte allocation
06-28 00:15:29.087    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 914K, 10% free 14088K/15495K, paused 1ms+3ms
06-28 00:15:29.127    5204-5230/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 19.443MB for 720016-byte allocation
06-28 00:15:29.237    5204-5228/com.test.app.d D/dalvikvm﹕ GC_FOR_ALLOC freed 1180K, 9% free 18274K/19911K, paused 24ms
06-28 00:15:29.297    5204-5228/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 26.879MB for 720016-byte allocation
06-28 00:15:29.347    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 344K, 4% free 22556K/23431K, paused 2ms+2ms
06-28 00:15:29.437    5204-5226/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 32.572MB for 720016-byte allocation
06-28 00:15:29.497    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 615K, 4% free 28883K/29959K, paused 3ms+4ms
06-28 00:15:29.577    5204-5228/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 38.750MB for 720016-byte allocation
06-28 00:15:29.617    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 545K, 4% free 33738K/34887K, paused 2ms+3ms
06-28 00:15:29.707    5204-5230/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 43.490MB for 720016-byte allocation
06-28 00:15:29.777    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 551K, 3% free 40900K/41927K, paused 2ms+3ms
06-28 00:15:29.877    5204-5228/com.test.app.d I/dalvikvm-heap﹕ Grow heap (frag case) to 50.485MB for 720016-byte allocation
06-28 00:15:29.937    5204-5206/com.test.app.d D/dalvikvm﹕ GC_CONCURRENT freed 613K, 3% free 45689K/46855K, paused 1ms+4ms

2 个答案:

答案 0 :(得分:0)

我知道我迟到了,但我喜欢解决这些问题。首先,你的问题仍然存在吗?

很少有建议 - &gt;

首先,您的GC比正常情况更频繁地中断。这也会中断主线程。所以我们只需要弄清楚为什么一次又一次地调用GC。

  • 首先检查图像尺寸。此外,您在磁盘上缓存可能是问题,因为每次从磁盘获取图像。
  • 尝试使用android MAT和层次结构查看器工具。可能会有所帮助。

我首先需要你的回答来弄清楚。

答案 1 :(得分:0)

通过简单地为低内存设备减少屏幕上的位图数量来解决这个问题。 Nostra在github网站上发布的解决方案有所帮助,但没有完全解决。