【发布时间】: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