为什么viewPager导致内存泄漏?

时间:2018-01-28 21:22:53

标签: android android-fragments memory-leaks android-viewpager android-pageradapter

在我的应用中,我在viewPager中使用,它使用FragmentStatePagerAdapter调整片段。 每个片段都包含gridView和GridViewAdapter。

一切正常但每次我交换时,内存使用量会增加很多并浪费内存。 为什么会发生这种情况,我该如何解决?

FragmentAdapter类:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.Calendar;


public class FragmentAdapter extends FragmentStatePagerAdapter {

    private static final int NUM_ITEMS = 12;


    public FragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case Calendar.JANUARY:
                return MonthFragment.newInstance(Calendar.JANUARY);
            case Calendar.FEBRUARY:
                return  MonthFragment.newInstance(Calendar.FEBRUARY);
            case Calendar.MARCH:
                return MonthFragment.newInstance(Calendar.MARCH);
            case Calendar.APRIL:
                return  MonthFragment.newInstance(Calendar.APRIL);
            case Calendar.MAY:
                return  MonthFragment.newInstance(Calendar.MAY);
            case Calendar.JUNE:
                return  MonthFragment.newInstance(Calendar.JUNE);
            case Calendar.JULY:
                return  MonthFragment.newInstance(Calendar.JULY);
            case Calendar.AUGUST:
                return  MonthFragment.newInstance(Calendar.AUGUST);
            case Calendar.SEPTEMBER:
                return  MonthFragment.newInstance(Calendar.SEPTEMBER);
            case Calendar.OCTOBER:
                return  MonthFragment.newInstance(Calendar.OCTOBER);
            case Calendar.NOVEMBER:
                return MonthFragment.newInstance(Calendar.NOVEMBER);
            case Calendar.DECEMBER:
                return MonthFragment.newInstance(Calendar.DECEMBER);
            default:
                throw new IllegalArgumentException("there is problem with position", new Throwable("NUM_ITEMS is not 12"));
        }
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

}

MonthFragment类:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


public class MonthFragment extends Fragment {

    private static final String ARG_MONTH = "arg_month";
    private int fragmentMonthId;

    private GridView gridView;
    private GridViewAdapter adapter;
    private ArrayList<Date> monthDates;

    public MonthFragment() {
        // Required empty public constructor
    }


    public static MonthFragment newInstance(int monthId) {
        MonthFragment fragment = new MonthFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_MONTH, monthId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            fragmentMonthId = getArguments().getInt(ARG_MONTH);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_month, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        gridView = view.findViewById(R.id.days);

        monthDates = getDates();
        adapter = new GridViewAdapter(getContext(), R.layout.day, 0, monthDates);

        gridView.setAdapter(adapter);
    }

    private ArrayList<Date> getDates() {
        ArrayList<Date> arrayList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, fragmentMonthId);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        for (int i = 0; i < calendar.getMaximum(Calendar.DAY_OF_MONTH); i++) {
            arrayList.add(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }
        return arrayList;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        monthDates.clear();
        adapter = null;
        gridView.setAdapter(null);
        gridView = null;
    }
}

GridViewAdapter类:

package com.example.kobimoshe.calendarwithpager;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class GridViewAdapter extends ArrayAdapter<Date> {

    ArrayList<Date> datesList;

    public GridViewAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Date> objects) {
        super(context, resource, textViewResourceId, objects);
        datesList = (ArrayList<Date>) objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.day, parent, false);
        }
        TextView dateView = (TextView) convertView.findViewById(R.id.date);
        dateView.setText(datesList.get(position).toString());

        return convertView;
    }

}

更新:

经过实验和研究,我假设了两件事:

  1. GC不会删除片段中的元素,即使我在片段被销毁时删除它们(我认为因为当调用onDestroyView时内存使用图不会减少)

  2. 每次交换都会造成内存泄漏(我已经使用ViewPager创建了一个新的,它使用FragmentStatePagerAdapter来设置空片段。每次在这个app中交换片段时内存使用量都会增加)。

  3. 任何人都可以向我解释为什么这些元素不能被完全删除?我怎样才能修复代码,以便在删除片段时清除它们? 即使片段以某种方式被删除或从中删除,也保存在内存中。如何从内存中删除片段以节省常量内存?

2 个答案:

答案 0 :(得分:0)

检查您的引用,99%的时间内存泄漏是由持久存在的对象引起的,这些对象应该是垃圾回收但仍然存在。确保您摆脱了不再使用的对象。

当您拥有大量片段时,FragmentStatePagerAdapter效果最佳,并且在页面之间切换时会增加一些开销。考虑尝试FragmentPagerAdapter,这对于较少量的片段更为可靠。如前所述,您应该实现viewholder pattern,因为您有一个视图列表。

答案 1 :(得分:0)

我已经成功解决了这个问题。

在onDetach方法的片段类中,我删除了所有子片段引用。然后,我调用Runtime.getRuntime().gc()以强制GC从未使用的元素中清理内存;