如何测试内存泄漏代码?

时间:2013-08-18 08:47:30

标签: java android memory-leaks garbage-collection

帮助了解是否存在内存泄漏 的片段

public class NewsFragment extends Fragment {
//some code
private OnNewsFeedsContentClickListener onNewsFeedsContentClickListener = new OnNewsFeedsContentClickListener()
{
    @Override
    public void onClick(String sYoutubeId, ContentType type)
    {
        //some code
    }
}

}
适配器

public class HowToAdapter extends BaseAdapter {

public static final String TAG = "HowToAdapter";
private List<ContentSkill> mContentList = null;
private Context mContext = null;
private ImageLoader mImageLoader = null;
private OnNewsFeedsContentClickListener mOnNewsFeedsContentClickListener = null;

public HowToAdapter(Context context, List<ContentSkill> contetnList, OnNewsFeedsContentClickListener onNewsFeedsContentClickListener)
{
    mContext = context;
    mContentList = contetnList;
    mImageLoader = new ImageLoader(mContext, ImageType.HOW_TO);
    mOnNewsFeedsContentClickListener = onNewsFeedsContentClickListener;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    HowToView view = null;
    if (convertView == null)
    {
        view = new HowToView(mContext);
    }
    else
    {
        view = (HowToView) convertView;
    }
    ContentSkill content = getItem(position);
    if (content != null)
    {
        ViewHolder holder = (ViewHolder) view.getTag();
        final String url = Utils.getYouTubeUrlImageFromUrl(content.getUrl());
        final String youtubeId = Utils.getYoutubeIdFromUrl(content.getUrl());
        mImageLoader.displayImage(url, holder.pIvThumb);
        holder.pIvThumb.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                mOnNewsFeedsContentClickListener.onClick(youtubeId, ContentType.VIDEO);
            }
        });
    }
    return view;
}

}
问题

  1. 将始终创建适配器变量 final String youtubeId , 但没有被杀?也就是说,每次都会创建getView()但是 旧的 youtubeId 内存不清楚?
  2. 每次调用getView()时都会创建OnClickListener,而旧的 将被清理掉内存?
  3. 我认为每次都会重新创建变量并追赶旧内存。

0 个答案:

没有答案