использование памяти увеличивается при прокрутке в программе повторного просмотра - PullRequest
0 голосов
/ 30 июня 2019

Я использую программу повторного просмотра, чтобы перечислить около 120 макетов, которые включают в себя 1 кнопку «Изображение», 1 «Просмотр текста» и 1 кнопку.Первоначальное использование памяти, когда я впервые запускаю активность, в которой есть обзор повторов, составляет около 50 МБ.Но когда я прокручиваю, он резко увеличивается и достигает 130-150 МБ, когда я добираюсь до конца списка.Я использую GridLayoutManager в качестве LayoutManager со счетчиком пролета 3.

Мои изображения довольно маленькие (только 200x200, размер всех 120 изображений составляет 612 КБ)

Что вызывает это увеличениеиспользования памяти?и как я могу решить эту проблему.Я получаю много отчетов о сбоях 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)

1 Ответ

0 голосов
/ 01 июля 2019

Есть несколько моментов, о которых нужно позаботиться:

  1. Нет необходимости делать класс viewHolder статическим.
  2. Не добавляйте «View.OnClickListener» в «onBindViewHolder». Вы можете использовать его в классе viewHolder. как это
public 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);

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