导航Arch组件会造成假阳性内存泄漏吗? [英] Could Navigation Arch Component create a false positive memory leak?

查看:100
本文介绍了导航Arch组件会造成假阳性内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对内存泄漏以及可能导致它们的原因有基本的了解.这就是为什么我不明白我的代码中是否有问题还是误报的原因.我不知道我应该共享代码的哪一部分,因为该项目并不小.但是请在评论中让我知道,然后我将添加所需的代码.

I have a basic knowledge of memory leaks and what can cause them. That's why I don't understand if I have a problem in my code or is it a false positive. I don't know which part of the code I should share since the project is not small. But just let me know in the comments and I will add required code.

我使用导航拱形组件并遵循MVVM模式.我在项目开发的后期添加了LeakCanary库,当我在屏幕之间导航时,它立即开始向我发出有关保留实例的警告.

I use navigation arch component and follow MVVM pattern. I added LeakCanary library later in the development of project and it immediately started to give me warnings about retained instances when I navigate between screens.

当我将片段添加到后堆栈时,会发生问题.随着将每个添加的片段添加到后向堆栈,保留实例的计数器将增加.当阈值达到5时,LeakCanary将转储堆并提供报告.

The problem occurs when I add fragments to the back stack. With each added fragment to the back stack the counter of retained instances increases. When it reaches the threshold value of 5 LeakCanary dumps the heap and provides report.

但是,如果我单击后退"按钮并返回到先前的屏幕,则保留实例的计数器会减少,最终,当返回到第一个屏幕时,所有保留的实例都会消失.

But if I click on back button and return to previous screens then counter of retained instances decreases and eventually, when returned to 1st screen all retained instances disappear.

如果我查看堆分析报告,它说该变量coordinatorLayout是xml中对CoordinatorLayout的引用.如果删除该变量及其所有用法,然后再次运行该应用程序,我会看到相同的问题,但是现在有了另一个变量,该变量引用了xml中的另一个视图.我试图删除LeakCanary报告为泄漏的所有视图及其用法.当它说TextView只是用于在onViewCreated中设置文本而未在其他任何地方使用时,正在泄漏,我开始怀疑我的代码是否有问题.

If I look at heap analysis reports it says that the variable coordinatorLayout which is a reference to the CoordinatorLayout in xml has leaked. If I remove the variable and all of its usage and run the app again I see the same problem, but now with another variable that is a reference to another view in xml. I tried to remove all of the views and their usage that LeakCanary reported as leaking. When it said that a TextView, which is just used to set a text in onViewCreated and not used anywhere else, is leaking I started to doubt that there is a problem in my code.

我分析了片段中的生命周期方法调用,并注意到当我导航到先前片段的新屏幕时,直到(包括)onDestroyView的所有方法都被调用,但没有调用onDestroy.当我单击后退onDestroy时,将调用位于后栈顶部且保留实例计数器减少的片段.

I analyzed the lifecycle method calls in fragments and noticed that when I navigate to new screen for previous fragment all methods till and including onDestroyView gets called but not onDestroy. When I click back onDestroy is called for fragment that was on top of back stack and retained instances counter decreases.

我怀疑导航组件在回栈中并且LeakCanary将其视为泄漏时,正在保留片段的实例.

I suspect that Navigation component is keeping the instance of a fragment when it is in back stack and LeakCanary is seeing it as a leak.

推荐答案

这就是后台堆栈上的Fragment的工作方式(而Navigation仅使用现有的Fragment API):Fragment的视图被破坏了,但是Fragment本身并未被破坏-它们一直保持在CREATED状态,直到您按下后退"按钮并返回到片段"(之后将再次调用onCreateView(),然后您将返回到RESUMED).

That's how Fragments on the back stack work (and Navigation just uses the existing Fragment APIs): the Fragment's view is destroyed, but the Fragment itself is not destroyed - they are kept in the CREATED state until you hit the back button and return to the Fragment (after which onCreateView() will be called again and you'll move back up to RESUMED).

根据片段:过去,现在和将来的谈话,这是未来的变化之一进入Fragments是一个选择选项,可以销毁后台堆栈上的Fragments,而不是具有两个单独的生命周期.目前尚不可用.

As per the Fragments: Past, Present, and Future talk, one of the future changes coming to Fragments is an opt in option to destroy Fragments on the back stack, rather than having two separate lifecycles. This isn't available as of yet.

您必须在onDestroyView中取消对视图的引用,因为这表明Fragment系统不再使用该视图,并且如果不继续引用该视图,则可以安全地对其进行垃圾回收.视图.

You have to null out your references to the views in onDestroyView as that's the sign that the view is no longer being used by the Fragment system and it can be safely garbage collected if it wasn't for your continued reference to the View.

这篇关于导航Arch组件会造成假阳性内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆