LiveData 在避免内存泄漏方面优于 Observer 对象
Advantage of LiveData Over Observer Object In Terms of Avoiding Memory Leak
我想了解 LiveData 是否比 Observable 对象在执行数据绑定时避免内存泄漏方面有任何优势。到目前为止,我了解到 LiveData 是一种可感知生命周期的可观察对象 class,但是根据 Android Data Binding Library — from Observable Fields to LiveData in two steps:
LiveData is lifecycle-aware but this is not a huge advantage with respect to Observable Fields because Data Binding already checks when the view is active.
我想澄清一下,因为 LiveData 的生命周期感知功能被吹捧为 class 避免内存泄漏的方法:
No memory leaks:
Observers are bound to Lifecycle objects and clean up after themselves when their associated lifecycle is destroyed.
正如我在评论和 article/documentation 中提到的,LiveData 只是 Observable 的一个更好版本。没有理由不使用 LiveData,除非您绝对确定不需要额外的附加值。
LiveData Overview explains how they avoid memory leaks by the system automatically cleaning them up when their associated Lifecycle (Fragment/Activity) object is destroyed. From the documentation of LiveData:
An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to DESTROYED state. This is especially useful for activities and fragments where they can safely observe LiveData and not worry about leaks: they will be instantly unsubscribed when they are destroyed.
如果您查看 Lifecycle.State,您会看到状态列表,它对应于 Activity/Fragment 中的各种 'on' 回调(例如 CREATED -> onCreate
)
资源泄漏可以定义为未能从系统中释放未使用的对象。 Android 中一种常见的内存泄漏类型是在其 Activity/Fragment (UI) 超出范围时保持对 Context 的引用。如果在后台任务、单例对象、静态变量或其他源中使用上下文,并且关联的 UI 对象超出范围,垃圾收集器将无法释放其内存,因为仍然存在至少有一个对 Context 的引用,导致内存泄漏。例如,假设您创建了一个执行某些后台任务(例如数据库访问)的 class,但它需要一个上下文。如果您的 UI 对象在该任务完成之前被销毁并且上下文未被取消引用,则会导致内存泄漏。
因此,总而言之,LiveData/Observable 对象处理样板代码,在不再需要它们时从关联的 Activity/Fragment 销毁或取消引用它们自己。
我想了解 LiveData 是否比 Observable 对象在执行数据绑定时避免内存泄漏方面有任何优势。到目前为止,我了解到 LiveData 是一种可感知生命周期的可观察对象 class,但是根据 Android Data Binding Library — from Observable Fields to LiveData in two steps:
LiveData is lifecycle-aware but this is not a huge advantage with respect to Observable Fields because Data Binding already checks when the view is active.
我想澄清一下,因为 LiveData 的生命周期感知功能被吹捧为 class 避免内存泄漏的方法:
No memory leaks: Observers are bound to Lifecycle objects and clean up after themselves when their associated lifecycle is destroyed.
正如我在评论和 article/documentation 中提到的,LiveData 只是 Observable 的一个更好版本。没有理由不使用 LiveData,除非您绝对确定不需要额外的附加值。
LiveData Overview explains how they avoid memory leaks by the system automatically cleaning them up when their associated Lifecycle (Fragment/Activity) object is destroyed. From the documentation of LiveData:
An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to DESTROYED state. This is especially useful for activities and fragments where they can safely observe LiveData and not worry about leaks: they will be instantly unsubscribed when they are destroyed.
如果您查看 Lifecycle.State,您会看到状态列表,它对应于 Activity/Fragment 中的各种 'on' 回调(例如 CREATED -> onCreate
)
资源泄漏可以定义为未能从系统中释放未使用的对象。 Android 中一种常见的内存泄漏类型是在其 Activity/Fragment (UI) 超出范围时保持对 Context 的引用。如果在后台任务、单例对象、静态变量或其他源中使用上下文,并且关联的 UI 对象超出范围,垃圾收集器将无法释放其内存,因为仍然存在至少有一个对 Context 的引用,导致内存泄漏。例如,假设您创建了一个执行某些后台任务(例如数据库访问)的 class,但它需要一个上下文。如果您的 UI 对象在该任务完成之前被销毁并且上下文未被取消引用,则会导致内存泄漏。
因此,总而言之,LiveData/Observable 对象处理样板代码,在不再需要它们时从关联的 Activity/Fragment 销毁或取消引用它们自己。