【问题标题】:FrameLayout and Bitmap Leaking Memory causes OOMFrameLayout 和 Bitmap Leaking Memory 导致 OOM
【发布时间】:2013-10-24 10:37:32
【问题描述】:

我正在从下面的这个片段中泄漏 FrameLayout 和 Bitmap 的内存。在框架布局上有弱引用是否明智?我想知道如何执行它。 holder.image 我也应该使用弱引用吗?我正在使用 Universal-Image-Loader 来加载大图像。

One instance of "android.widget.FrameLayout" loaded by "<system class loader>" occupies 1,786,072 (13.27%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "<system class loader>".

Keywords
android.widget.FrameLayout
byte[]

43 instances of "android.graphics.Bitmap", loaded by "<system class loader>" occupy 1,897,648 (14.10%) bytes. 

Biggest instances:
•android.graphics.Bitmap @ 0x40e73900 - 1,048,656 (7.79%) bytes. 
•android.graphics.Bitmap @ 0x40e79280 - 281,960 (2.09%) bytes. 


Keywords
android.graphics.Bitmap

片段

   @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    //final View view;
    context = getActivity();
    holder hold = new holder();

    View  view = (FrameLayout) getActivity().getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);

    //int imageWidth = loadedImage.getWidth();
    //int imageHeight = loadedImage.getHeight();


    if (convertView == null) {
        //view = (LinearLayout) inflaterr.inflate(R.layout.item_grid_image, parent, false);
        hold.image = (ImageView)view.findViewById(R.id.image);


    } else {
        view = (View) convertView;
        hold.image = (ImageView)view.findViewById(R.id.image);



        imageLoader.displayImage(values.get(position).get("imagePath").toString(),
        hold.image, options, animateFirstListener );

    }

class holder
        {
            ImageView image;

        }

【问题讨论】:

    标签: java android memory memory-management memory-leaks


    【解决方案1】:

    你总是在膨胀一个新的FrameLayout,即使你没有使用它(当convertView != null时)。仅当convertView 为空时才对其进行充气。

    另外,android 开发者网站上的managing bitmap memory 也有很好的资源。

    编辑:

    取自readme at Android-Universal-Image-Loader project

    如果您在使用 Universal Image 的应用中经常遇到 OutOfMemoryError 加载器然后尝试下一个(全部或几个):

    • 减少配置中的线程池大小 (.threadPoolSize(...))。建议使用 1 - 5。

    • 在显示选项中使用 .bitmapConfig(Bitmap.Config.RGB_565)。 RGB_565 中的位图消耗的内存是 ARGB_8888 中的 2 倍。

    • 在配置中使用 .memoryCache(new WeakMemoryCache()) 或在显示选项中完全禁用内存中的缓存(不要调用
      .cacheInMemory())。

    • 在显示选项中使用 .imageScaleType(ImageScaleType.IN_SAMPLE_INT)。或尝试 .imageScaleType(ImageScaleType.EXACTLY)。

    • 避免使用 RoundedBitmapDisplayer。它使用 ARGB_8888 配置创建新的位图对象,以便在工作期间显示。

    【讨论】:

    • 谢谢!!为 Framelayout 节省了很多内存!!!我正在为我的图像使用 Universal-Image-Loader,根据您提供的链接,我可以用 UIl 处理我的图像吗?谢谢
    • 我有那个配置,先生,所有的。使用 UIL 对 ImageView 进行弱引用怎么样?
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多