我已经有这个问题一段时间了,我真的不知道还有什么可以尝试的。我把 Leakcanary 卡在我的项目上,这样我就可以强迫自己以“正确的方式”学习 android,而不会发现导致内存泄漏的模式。我正在构建一个应用程序,我已经把它剥离了很多,但我仍然看到这种泄漏发生。
我有一个 Activity,里面有一个 viewpager 并实例化了 10 个片段。如果我打开设备一切似乎都很好,直到我按下“主页”,我才得到以下内存泄漏:
D/LeakCanary﹕ In com.doesnthaveadomain.leo.calendartracker:1.0:1.
D/LeakCanary﹕ * com.doesnthaveadomain.leo.calendartracker.MyFragment has leaked:
D/LeakCanary﹕ * GC ROOT static android.view.WindowManagerGlobal.sDefaultWindowManager
D/LeakCanary﹕ * references android.view.WindowManagerGlobal.mRoots
D/LeakCanary﹕ * references java.util.ArrayList.array
D/LeakCanary﹕ * references array java.lang.Object[].[0]
D/LeakCanary﹕ * references android.view.ViewRootImpl.mInvalidateOnAnimationRunnable
D/LeakCanary﹕ * references android.view.ViewRootImpl$InvalidateOnAnimationRunnable.mViews
D/LeakCanary﹕ * references java.util.ArrayList.array
D/LeakCanary﹕ * references array java.lang.Object[].[0]
D/LeakCanary﹕ * references android.support.v4.view.ViewPager.mAdapter
D/LeakCanary﹕ * references com.doesnthaveadomain.leo.calendartracker.MyAdapter.mCurrentPrimaryItem
D/LeakCanary﹕ * leaks com.doesnthaveadomain.leo.calendartracker.MyFragment instance
主要活动:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.lang.ref.WeakReference;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), this);
mViewPager.setAdapter(myAdapter);
mViewPager.setCurrentItem(5);
}
}
class MyAdapter extends FragmentStatePagerAdapter {
private int mNumberOfViews;
private final WeakReference<AppCompatActivity> mActivityWeakRef;
public MyAdapter(FragmentManager fm,
AppCompatActivity activity) {
super(fm);
mActivityWeakRef = new WeakReference<AppCompatActivity>(activity);
mNumberOfViews = 10;
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = new MyFragment();
AppCompatActivity activity = mActivityWeakRef.get();
if (activity != null) {
MyApp.getRefWatcher(mActivityWeakRef.get()).watch(myFragment);
}
return myFragment;
}
@Override
public int getCount() {
return mNumberOfViews;
}
}
MainActivity 布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/AppTheme.Toolbar"
app:popupTheme="@style/AppTheme.Toolbar.Popup">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
分段:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}
}
片段布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_vertical_margin"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
</LinearLayout>