首页 文章

Glide和recyclerview,记忆太多

提问于
浏览
0

我使用Glide下载图像(每个~4Kb)和带有gridlayout的recyclerview来显示它 . 我将图像url存储在lru缓存中 . 在onBindViewHolder()中,我得到了图片网址,并将其与Glide一起显示如下:

Glide.with(mMainActivity).load(item.getPosterPath()).thumbnail(0.5f).into(holder.posterPath);

问题是,在300张图片后,应用程序进入内存不足异常,这是Android分析器信息:
enter image description here

这是我的recyclerview适配器:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_content, parent, false));
}



@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    Item item = mItems.get(position);
    if (item!= null) {
        holder.itemView.setTag(item);
        Glide.with(mMainActivity).load(item.getPosterPath()).apply(ro).thumbnail(0.5f).into(holder.posterPath);

        holder.itemView.setOnClickListener(mOnClickListener);
    }
}

class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView posterPath;
    private TextView originalName;

    ViewHolder(View view) {
        super(view);
        posterPath = itemView.findViewById(R.id.poster);
    }
}

这是我的结构:

public static LruCache<Integer, Item> mItemMap = new LruCache<>(20);

项目是:

public class Item {
    private String mId;
    private String mOriginalName;
    private String mFirstAirDate;
    private String mLanguage;
    private String mOverview;
    private String mPosterPath;
    private int itemPos;

    Item(String id, String originalName, String firstAirDate, String language, String posterPath, String overview, int itemPos) {
        this.mId = id;
        this.mOriginalName = originalName;
        this.mFirstAirDate = firstAirDate;
        this.mLanguage = language;
        this.mOverview = overview;
        this.mPosterPath = posterPath;
        this.itemPos = itemPos;
    }

    /**
     * Getters.
     */
}

我的recyclerview滚动适配器:

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mGridLayoutManager.getItemCount();
    firstVisibleItem = mGridLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
        if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
        }
    }
    if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
        current_page++;
        onLoadMore(current_page);

        loading = true;
    }
}

这就是我从互联网获取新数据的方式:

public void newItem() {
    currentPage++;
    String URL = "myurl";
    RestClient.get(URL, null, mHandler);
}

mHandler在哪里:

mHandler = new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {
                results = (JSONArray) response.get("results");
                for (int i = 0; i < results.length(); i++) {
                    obj = results.getJSONObject(i);

                    id = obj.get("id").toString();
                    posterPath = obj.get("poster_path").toString();

                    if (!posterPath.equals("null")) {
                        mItem.add(ItemList.createItem(id, null, null, null, POSTER_BASE_URL+posterPath, null, itemPos));
                        itemPos++;
                    }
                }
                notifySubscriber();
            } catch (JSONException e) {
                Log.d("JSON", "Error in parsing json.");
            }
        }
    };

这是我的RestClient:

private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.get(getAbsoluteUrl(url), params, responseHandler);

}

enter image description here

所有byte []分配的位置和时间?

1 回答

  • 2

    我真的不知道会发生什么,但有两件事我会留意:

    您正在传递或使用 mMainActivity ,即我假设您对 Activity 的引用 . 绝对没有必要在那里引用整个Activity . 您有视图,并且您正在视图的上下文中加载图像(您绑定的ViewHolder),因此从那里删除该泄漏 .

相关问题