【问题标题】:Memory Leaks in RecyclerView when used in Fragment by LeakCanaryLeakCanary 在 Fragment 中使用时 RecyclerView 中的内存泄漏
【发布时间】:2017-10-26 14:50:45
【问题描述】:

片段中带有 recyclerview 的简单应用程序根据泄漏应用程序存在内存泄漏。

Memory Leak snapshot by Leak App

泄漏堆栈:

HelloTest.java:

public class HelloTest extends AppCompatActivity {
    private TestFrag mFrag = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            mFrag = new TestFrag();
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(android.R.id.content, mFrag )
                    .commit();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = AppClass.getRefWatcher(this);
        refWatcher.watch(this);

    }
}

片段:

public class TestFrag extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_hello_test, container, false);

        return view;
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/comme_rv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

通过执行上述应用程序代码,当应用程序退出时发生内存泄漏。 如果将相同的代码放置在活动中而不是片段中,则没有泄漏。 难道我做错了什么?

【问题讨论】:

    标签: android android-fragments memory-leaks android-recyclerview leakcanary


    【解决方案1】:

    您不应该在 onDestroy 方法中创建任何东西。所以你把你的代码是这样的:

    public class HelloTest extends AppCompatActivity {
        private TestFrag mFrag = null;
       RefWatcher refWatcher;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            refWatcher = AppClass.getRefWatcher(this);
            refWatcher.watch(this);
            if (savedInstanceState == null) {
                mFrag = new TestFrag();
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(android.R.id.content, mFrag )
                        .commit();
            }
        }
    
        @Override
        public void onDestroy() {
    
            if(refWatcher!=null){
              // destroy refWatcher
            }
            super.onDestroy();
        }
    }
    

    【讨论】:

    • 感谢您的建议,我已经厌倦了这个,但问题仍然存在。
    • 请把您尝试过的代码放入 onDestroy。试试这个: public void onBackPressed() { if(refWatcher!==null){ // 销毁 refWatcher } super.onBackPressed();}
    • 我删除了 onDestroy 方法。问题依然存在
    • 你能把你的代码上传到 Github 吗? ...我会检查它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    相关资源
    最近更新 更多