我一直遇到这个问题,但真的不知道还能尝试什么。我在项目中使用了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;
}
}
主活动布局:
<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>