在Android片段上获取内存泄漏

时间:2015-05-07 21:47:24

标签: android gridview android-fragments memory-leaks android-arrayadapter

我一直在寻找我的Android应用程序上的“内存泄漏”问题的解决方案,但是,这是事情:
我正在viewpager中加载一个片段,这个片段有一个Gridview和一个Adapter(ArrayAdapter),它创建gridview的每个项目,这些项目有一个图像和一些文本(我使用Universal Image Loader)来加载图像。)

当我改变设备方向时,我会收到很多内存泄漏。

  • 我已将所有适配器上下文更改为ApplicationContext。
  • 我已将图像加载器插件(Universal Image Loader)更改为 Picasso
  • 我已将ArrayAdapter更改为BaseAdapter。
  • 我已将Gridview更改为FlowLayoutView,并将所有项目作为View添加到此新ViewGroup。
  • 在没有使用android调试的情况下进行了测试。

这些更改中没有一项有效,我仍然会收到内存泄漏,欢迎任何帮助!

编辑:

public class MoviesFragment extends Fragment {

public MoviesFragment() {
}

final String TAG = MoviesFragment.class.toString();
ExpandableHeightGridView movieGridView;
MovieAdapter mMovieAdapter;
ArrayList<Movie> movies;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    movies = args.getParcelableArrayList("movies");
    mMovieAdapter = new MovieAdapter(getActivity().getApplicationContext(), movies);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
    movieGridView = (ExpandableHeightGridView) rootView.findViewById(R.id.gridview_movies);

    if (movies != null) {

        movieGridView.setAdapter(mMovieAdapter);
        movieGridView.setExpanded(true);
        movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EventBus.getDefault().post(
                        new EventSelectedEvent((Movie) parent.getItemAtPosition(position)));
            }
        });
    } else {
        movieGridView.setExpanded(false);
    }
    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

}

public class MovieAdapter extends ArrayAdapter<Movie> {

Context context;

public MovieAdapter(Context context, List<Movie> movies) {
    super(context, 0, movies);
    this.context = context.getApplicationContext();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Movie movie = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext().getApplicationContext()).inflate(R.layout.layout_card_movies, parent, false);
    }
    // Lookup view for data population
    TextView movieName = (TextView) convertView.findViewById(R.id.card_movie_text);

    //Load square image
    Image squareImg = movie.getSquareImage();
    if (null != squareImg) {
        ImageLoader iLoader = ImageLoaderHandler.getImageLoader(TandasApplication.getInstance());
        iLoader.displayImage(squareImg.getLink(),
                (ImageView) convertView.findViewById(R.id.card_movie_image));
    }

    //Title
    movieName.setText(movie.getTitle());

    //Rating
    LinearLayout ratingContainer = (LinearLayout) convertView.findViewById(R.id.ratingMovieMainView);
    double rating = movie.getRating();
    UIUtils.buildRatingStars(rating, ratingContainer, context);

    // Return the completed view to render on screen
    return convertView;
}

}

1 个答案:

答案 0 :(得分:0)

如果没有太多信息,我认为你正在坚持你的片段引用。

使用adb shell命令dumpsys

进行测试
adb shell dumpsys activity <package_name>
adb shell dumpsys meminfo <package_name>

导航各种片段并在每次导航其中之后运行此片段。 Meminfo将告诉您应用程序持有多少活动。活动为你打破了一切。

如果您发布一些日志或代码,我可以帮助您更好。