繁体   English   中英

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

[英]GridView Out of Memory Universal Image Loader

我的许多用户(主要是使用Samsung设备)在使用gridview的应用程序中遇到“内存不足”错误。 请注意,我无法在任何设备(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

我知道我来晚了,但我喜欢解决这类问题。 首先,您的问题仍然存在吗?

很少建议->

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

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

我首先需要您的答案才能弄清楚更多。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM