gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:02:42 24 4
gpt4 key购买 nike

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

我的图片很小(只有 200x200,所有 120 张图片的大小是 612 KB)

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

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

在 Emotes Activity 中初始化我的 recyclerview:

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)

最佳答案

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

关于java - 在 recyclerview 中滚动时内存使用量增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823590/

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