gpt4 book ai didi

java - FrameLayout 和 Bitmap 内存泄漏导致 OOM

转载 作者:行者123 更新时间:2023-11-30 03:18:41 25 4
gpt4 key购买 nike

我从下面的这个 fragment 中泄漏了 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

fragment

   @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;

}

最佳答案

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

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

编辑:

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

If you often got OutOfMemoryError in your app using Universal Image Loader then try next (all of them or several):

  • Reduce thread pool size in configuration (.threadPoolSize(...)). 1 - 5 is recommended.

  • Use .bitmapConfig(Bitmap.Config.RGB_565) in display options. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.

  • Use .memoryCache(new WeakMemoryCache()) in configuration or disable caching in memory at all in display options (don't call
    .cacheInMemory()).

  • Use .imageScaleType(ImageScaleType.IN_SAMPLE_INT) in display options. Or try .imageScaleType(ImageScaleType.EXACTLY).

  • Avoid using RoundedBitmapDisplayer. It creates new Bitmap object with ARGB_8888 config for displaying during work.

关于java - FrameLayout 和 Bitmap 内存泄漏导致 OOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19563593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com