android 即使在回收位图后仍发生内存不足异常

Out Of Memory Exception occuring in android even after recycling Bitmap

我已经阅读了很多关于这个主题的帖子,并尝试在我的代码中使用它们,但我仍然无法解决它。

我正在尝试做的事情: 就像 facebook 或任何其他从服务器下载包含图像的提要的应用程序一样,我也在做同样的事情并尝试在图像视图上显示提要和图像。

发生了什么: 我能够下载包含 URL 图像的提要(在 JSON 中),并且我能够在 ImageView 上显示它们。 在模拟器中,我的最大堆大小是 64MB。我在前 10 个 feed 中消耗了大约 30MB(不知道为什么,但这是我从 Android Studio 的 Android 监视器甚至 Android 设备监视器中的内存选项卡中得到的)。 我的应用程序中有一个刷新按钮,它会在删除之前填充的所有提要后重新加载相同的提要。我预计我会消耗相同的内存或更多内存。但与此相反,我的内存使用量增加到 42MB。因此,点击刷新 3 到 4 次后,会导致 OutOFMemory Execption。即使我一次加载下 10 个提要或 50 个提要,我也会遇到内存不足异常。

我知道 facebook instagram 和更多此类应用程序做同样的事情,但不确定他们如何实现代码来应对这种情况。

下面是我用于填充提要的代码

private void loadFeed(List<Feed> feedList)
{
    Log.v(Constant.TAG,"Loading Feed in social feed page");
    for(final Feed feed:feedList) {
        LinearLayout feedBox = new LinearLayout(this);
        feedBox.setOrientation(LinearLayout.VERTICAL);
        FrameLayout profileDetailContainer= new FrameLayout(this);
        LinearLayout profileDetailContainerParent=new LinearLayout(this);
        LinearLayout profileDetailContainerChild=new LinearLayout(this);
        profileDetailContainerChild.setOrientation(LinearLayout.VERTICAL);
        ImageView imgProfile= new ImageView(this);
        TextView txtDate= new TextView(this);
        TextView txtName= new TextView(this);
        ImageView imgProduct= new ImageView(this);
        txtName.setText(feed.getUserFullName());
        TextView txtDesciption= new TextView(this);
        txtName.setTypeface(null, Typeface.BOLD);
        if(feed.getDescription().length()>Constant.NUMBER_OF_DESCRIPTION_CHAR_SHOW_ON_RESULT)
        {
            txtDesciption.setText(feed.getDescription().substring(0,Constant.NUMBER_OF_DESCRIPTION_CHAR_SHOW_ON_RESULT)+"...");
        }
        else
            txtDesciption.setText(feed.getDescription());

        if(!IOUtil.fileExists(this,feed.getProductImageName())) {
            WebRequest request = new WebRequest();
            request.setUrl(Constant.ROOT_APPLICATION_URL_WITH_SEPARATOR + feed.getImgPath());
            request.setParam(feed.getId() + "");
            new ImageDownloadTask(this, true, feed.getProductImageName(), this).execute(request);
            Log.v(Constant.TAG,"URL:"+Constant.ROOT_APPLICATION_URL_WITH_SEPARATOR+feed.getImgPath());
            Picasso.with(getApplicationContext()).load(R.drawable.logo).into(imgProduct);
            feedBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ScreenUtility.alertException(v.getContext(),"Please wait untill product image loads");
                }
            });
            PixyfiSession.save(feed.getId() + "", imgProduct);
            PixyfiSession.save(feed.getId() + "_feed", feed);

        }
        else
        {
            ImageUtil.recycleIfPossible(imgProduct);
            try {
                imgProduct.setImageBitmap(ImageUtil.getLocalImage(feed.getProductImageName(),this));
                FeedboxOnClickListener feedboxOnClickListener = new FeedboxOnClickListener(feed);
                feedBox.setOnClickListener(feedboxOnClickListener);
            } catch (FileNotFoundException e) {
                Log.v(Constant.TAG,e.getMessage(),e);
            }

        }

        if(FacebookUtil.localProfilePicExists(feed.getUserName(),this))
        {
            Bitmap profileImage= FacebookUtil.getLocalProfilePicture(feed.getUserName(),this);
            imgProfile.setImageBitmap(profileImage);
        }
        else {
            FacebookUtil.loadProfilePicture(feed.getUserName(),this,this);
            PixyfiSession.save(feed.getUserName(),imgProfile);
            imgProfile.setImageResource(R.drawable.profile);
        }
        try {

            if(feed.getDate()==null) {
                txtDate.setText(new SimpleDateFormat("dd MMM yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse(feed.getFeedDate())));
            }
            else {
                txtDate.setText(new SimpleDateFormat("dd MMM yyyy").format(feed.getDate()));
            }
        } catch (ParseException e) {
            Log.e(Constant.TAG,e.getMessage(),e);
        }
        LinearLayout.LayoutParams layoutParam= new LinearLayout.LayoutParams(140,140);
        layoutParam.setMargins(5,5,0,0);
        imgProfile.setLayoutParams(layoutParam);

        layoutParam= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.TOP|Gravity.LEFT);
        layoutParam.setMargins(20,5,0,0);
        txtName.setLayoutParams(layoutParam);
        layoutParam= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,Gravity.LEFT);
        layoutParam.setMargins(20,5,0,0);
        txtDate.setLayoutParams(layoutParam);

        profileDetailContainerParent.addView(imgProfile);
        profileDetailContainerChild.addView(txtName);
        profileDetailContainerChild.addView(txtDate);
        profileDetailContainerParent.addView(profileDetailContainerChild);
        feedBox.addView(profileDetailContainerParent);
        LinearLayout.LayoutParams feedLayoutParam=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL);
        feedLayoutParam.setMargins(5,5,5,5);
        imgProduct.setLayoutParams(feedLayoutParam);
        txtDesciption.setLayoutParams(feedLayoutParam);
        feedBox.addView(txtDesciption);
        feedBox.addView(imgProduct);
        feedLayoutParam=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL);
        feedLayoutParam.setMargins(0,5,0,5);
        feedBox.setLayoutParams(feedLayoutParam);

        feedBox.setBackgroundColor(Color.parseColor("#cccccc"));
        PixyfiSession.save(feed.getId()+"_feedbox",feedBox);
        imgProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PixyfiSession.save(Constant.SELECTED_USER_ID,feed.getUserName());
                ScreenUtility.loadScreen(v.getContext(),UsersProfile.class,false);
            }
        });
        this.feedContainer.addView(feedBox);

    }
    if(feedList.size()==Constant.FEED_SIZE_IN_ONE_REQUEST)
    {
        Button moreFeed= new Button(this);
        moreFeed.setText("Load MOre Feed");
        moreFeed.setOnClickListener(new MoreFeedButtonListener(this));
        this.feedContainer.addView(moreFeed);
    }
    this.progressBar.setVisibility(View.INVISIBLE);
}

对于 reloading/refreshing 供稿

this.currentPage=1;
this.recycleImages();
this.feedContainer.removeAllViews();
PixyfiSession.save(Constant.CURRENT_FEED_PAGE,this.currentPage);
this.progressBar.setVisibility(View.VISIBLE);
loadFeed();

recycleImages 方法:

private void recycleImages()
{
    for(int i=0;i<this.feedContainer.getChildCount();i++)
    {
        if(this.feedContainer.getChildAt(i) instanceof  ImageView)
        {
            ImageView view=(ImageView)this.feedContainer.getChildAt(i);
            ImageUtil.recycleIfPossible(view);
        }
    }
}

如果您需要有关代码的更多详细信息,请告诉我。 也可以在 android 设备监视器中查看其他应用程序(如 facebook)的内存使用情况吗? 更新 ImageUtil.getLocalImage 方法

public static Bitmap getLocalImage(String imageName, Context context) throws FileNotFoundException
{
    Bitmap bitmap=null;
    InputStream is=null;
    if(IOUtil.fileExists(context,imageName)) {
        is = context.openFileInput(imageName);
        bitmap= BitmapFactory.decodeStream(is);
    }
    else {
        throw new FileNotFoundException("Image file doesn't exists");
    }
    return bitmap;
}

我有同样的问题将此行添加到应用程序标签中的清单:

    <application
        android:largeHeap="true"
        .
        .
        .

正如 告诉的那样,先这样做然后添加

 defaultConfig {
    ....
    multiDexEnabled true
    ....
}

在你的

app/build.gradle

文件..

您似乎在加载图片时遇到了问题。请检查您的ImageUtils.getLocalImages,您是将图像的缩放版本解码并加载到内存中,还是将原始图像加载到内存中?

关注 this document from Android developer ,我认为这是在 Android.

中高效加载图像的最佳教程

Android 中的图像处理非常棘手。更好地使用已构建的库来处理所涉及的情况和复杂性。 我更喜欢 UniversalImageLoader 或 Piccaso!

这是因为您在 运行 时间内存中保留了太多位图图像,请尽量不要创建太多图像并尝试使它们无效。

移动设备通常具有受限的系统资源。 Android 设备可以为单个应用程序提供低至 16MB 的可用内存。虚拟机兼容性为各种屏幕尺寸和密度提供了所需的最小应用程序内存。应优化应用程序以在此最小内存限制下执行。但是,请记住,许多设备都配置了更高的限制。

我不会向您推荐 android 清单中的 heapSize 标志。我希望您不是在开发游戏 :),而是根据需要进行改造或任何其他图像处理库。我建议您查看 Google 提供的 BitmapFun 示例。它可以在开发者门户网站上找到。

http://developer.android.com/training/displaying-bitmaps/index.html

干杯!!

我正在添加答案:

与其自己管理视图组件,不如选择列表视图或更好的回收视图。 因为它可以帮助您避免视图泄漏并更好地重用您已创建的视图。