在recyclerview中滚动时,内存使用量增加

时间:2019-06-30 08:52:31

标签: java android android-recyclerview memory-leaks out-of-memory

我正在使用recyclerview列出大约120种布局,其中包括1个Imagebutton,1个Textview和1个Button。当我第一次启动内部具有recyclerview的活动时,初始内存使用量约为50mb。但是当我滚动时,它急剧增加,到达列表末尾时达到130-150mb。 我正在使用GridLayoutManager作为我的LayoutManager,其跨度计数为3。

我的图像很小(只有200x200,所有120张图像的大小均为612 KB)

是什么导致内存使用量增加?以及我该如何解决这个问题。我收到很多OutOfMemory崩溃报告,主要是在Android 6中发生的。 谢谢您的宝贵时间。

*我尝试使用像Picasso这样的第三方库来代替设置后台资源。 *我已经尝试使用位图工厂解码图像

在Emotes活动中初始化我的回收站视图

recyclerView = findViewById(R.id.recyclerview);
            adapter = new Adapter(arr, this);
            adapter.setHasStableIds(true);
            recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
            recyclerView.setHasFixedSize(true);
            recyclerView.setItemViewCacheSize(20);


            recyclerView.setAdapter(adapter);

我的适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

    private CommonMethods commonMethodsInterface;
    private ArrayList<Emote> emoteArray ;
    private ArrayList<Emote> emoteArrayFull;



    public Adapter(ArrayList<Emote> emoteArray,CommonMethods commonMethodsInterface) {
        this.emoteArray = new ArrayList<>(emoteArray);
        this.emoteArrayFull = new ArrayList<>(emoteArray);

        this.commonMethodsInterface = commonMethodsInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_listitem,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {

        viewHolder.emoteButton.setBackgroundResource(emoteArray.get(i).getIconPath());
        viewHolder.text.setText(emoteArray.get(i).getName().toUpperCase());

        viewHolder.emoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseConstants.INTERSTITIAL_AD_COUNTER++;
                commonMethodsInterface.ShowInterstitialAd();
                commonMethodsInterface.playEmoteSound(emoteArray.get(i));



         }
        });
        viewHolder.optionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseConstants.INTERSTITIAL_AD_COUNTER++;
                commonMethodsInterface.ShowInterstitialAd();
                commonMethodsInterface.ShowDialog(emoteArray.get(i));
            }
        });
    }


    @Override
    public int getItemCount() {
        return emoteArray.size();
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }
 public static class ViewHolder extends RecyclerView.ViewHolder{

        ImageButton emoteButton;
        Button optionButton;
        TextView text;



        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            emoteButton = itemView.findViewById(R.id.tempimagebutton);
            optionButton = itemView.findViewById(R.id.tempbutton);
            text = itemView.findViewById(R.id.temptext);


        }
    }
}
  

Stacktrace

java.lang.OutOfMemoryError: 
  at dalvik.system.VMRuntime.newNonMovableArray (Native Method)
  at android.graphics.BitmapFactory.nativeDecodeAsset (Native Method)
  at android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:709)
  at android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:530)
  at android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:1089)
  at android.content.res.Resources.loadDrawableForCookie (Resources.java:2963)
  at android.content.res.Resources.loadDrawable (Resources.java:2848)
  at android.content.res.HwResources.loadDrawable (HwResources.java:712)
  at android.content.res.Resources.getDrawable (Resources.java:970)
  at android.content.Context.getDrawable (Context.java:463)
  at android.view.View.setBackgroundResource (View.java:17562)
  at android.support.v7.widget.AppCompatImageButton.setBackgroundResource (AppCompatImageButton.java:114)
  at com.nullbytesoftware.example.Adapter.onBindViewHolder (Adapter.java:42)
 **at com.nullbytesoftware.example.Adapter.onBindViewHolder (Adapter.java:18)
  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder (RecyclerView.java:6781)**
  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder (RecyclerView.java:6823)
  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline (RecyclerView.java:5752)
  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6019)
  at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline (GapWorker.java:286)
  at android.support.v7.widget.GapWorker.flushTaskWithDeadline (GapWorker.java:343)
  at android.support.v7.widget.GapWorker.flushTasksWithDeadline (GapWorker.java:359)
  at android.support.v7.widget.GapWorker.prefetch (GapWorker.java:366)
  at android.support.v7.widget.GapWorker.run (GapWorker.java:397)
  at android.os.Handler.handleCallback (Handler.java:819)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loop (Looper.java:210)
  at android.app.ActivityThread.main (ActivityThread.java:5982)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:852)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:742)

2 个答案:

答案 0 :(得分:1)

我不知道这是一个怪异的错误还是实现Recyclerview时我做错了什么,但是设置了viewHolder.setIsRecyclable(false);让我的RecyclerView开始回收元素并解决了我的内存问题。仍然很想知道这种相反行为的原因。

答案 1 :(得分:0)

有几点需要注意:

  1. 无需将viewHolder类设置为静态。
  2. 不要在“ onBindViewHolder”中添加“ View.OnClickListener”。您可以在viewHolder类中使用它。像这样
PIL