我使用Glide下载图像(每个~4Kb)和带有gridlayout的recyclerview来显示它。我将图像url存储在lru缓存中。在onBindViewHolder()中,我得到了图片网址,并将其与Glide一起显示如下:
Glide.with(mMainActivity).load(item.getPosterPath()).thumbnail(0.5f).into(holder.posterPath);
问题是,在300张图片后,应用程序进入内存不足异常,这是Android分析器信息:
这是我的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);
}
在何时何地进行所有byte []分配?
答案 0 :(得分:3)
我真的不知道会发生什么,但有两件事我会留意:
Glide.with(holder.getContext()).load(…
。您正在传递或使用mMainActivity
,即我假设您对Activity
的引用。绝对没有必要在那里引用整个Activity。你有视图,你正在视图的上下文中加载图像(你绑定的ViewHolder),所以从那里删除泄漏。