【Leakcanary】Introduction

reference from :https://square.github.io/leakcanary/fundamentals/

fundamentals   n. 基本面;基本原理(fundamental的复数)
hesitate  vt. 踌躇,犹豫;有疑虑,不愿意
confusing section 令人困惑的部分
reclaimed  v. 回收利用;改造(reclaim的过去分词);开垦土地
prevents  v. 阻止(prevent的第三人称单数);预防
lifecycle n. 生活周期;生活过程;生命循环
Common adj. (common) 常见的,普通的
survives  vt. 幸存;生还;幸免于;比...活得长
reaches v. 伸出;到达(reach第三人称单数)
accumulation n. 积聚,累积;堆积物
correctly adv. 正确地;得体地
distinct adj. 明显的;独特的;清楚的;有区别的


prevent...from doing sth	阻止...做某事, 防止...做某事
due to 由于

The fundamentals describe how LeakCanary works and how to use it to detect and fix memory leaks. This documentation is designed to help developers of all levels, so please don’t hesitate to report any confusing section.

最基础的描述LeakCanary的工作原理以及怎样使用它修复内存泄漏,这个文档被设计来用于帮助所有等级在这里插入代码片的开发者,所以不要对报告和不清楚的部分产生疑虑。

What is a memory leak?
In a Java based runtime, a memory leak is a programming error that causes an application to keep a reference to an object that is no longer needed. As a result, the memory allocated for that object cannot be reclaimed, eventually leading to an OutOfMemoryError (OOM) crash.

什么是内存泄漏
在Java的基本运行中,内存泄漏是编程中的错误,他会导致应用持有一个已经不再使用的对象的引用。结果是,被那个对象创建的引用不可以被回收,甚至会导致一个内存溢出(OOM)的异常。

For example, an Android Activity instance is no longer needed after its onDestroy() method is called, and storing a reference to that instance in a static field prevents it from being garbage collected.

例如,一个Android Activity的实例当onDestroy()函数被调用后就不再被需要,一个静态域实例的强引用会阻止垃圾的回收。

Common causes for memory leaks
Most memory leaks are caused by bugs related to the lifecycle of objects. Here are a few common Android mistakes:

内存泄漏的一般原因
绝大多数的内存泄漏导致的原因是和生命周期有关的漏洞,以下是几个常见的Android错误。

1、Adding a Fragment instance to the backstack without clearing that Fragment’s view fields in Fragment.onDestroyView() (more details in this StackOverflow answer).
2、Storing an Activity instance as a Context field in an object that survives activity recreation due to configuration changes.
3、Registering a listener, broadcast receiver or RxJava subscription which references an object with lifecycle, and forgetting to unregister when the lifecycle reaches its end.

1、添加一个Fragment的实例到栈中没有在Fragment.onDestrouyView()中清除Fragment的视图字段(更多详情查看栈栈溢出的回答)。2、由于配置的改变重新创建一个Activity对象时把Activity作为一个Context的字段储存。3、注册一个监听器、广播接收器或者RxJava订阅引用到拥有生命周期的对象,并且在对象的生命周期结束时忘记取消注册。

Why should I use LeakCanary?
Memory leaks are very common in Android apps and the accumulation of small memory leaks causes apps to run out of memory and crash with an OOM. LeakCanary will help you find and fix these memory leaks during development. When Square engineers first enabled LeakCanary in the Square Point Of Sale app, they were able to fix several leaks and reduced the OOM crash rate by 94%.

为什么我需要使用LeakCanary
内存泄漏在安卓应用中是非常常见的,同时很小的内存泄漏堆积会使应用内存溢出导致OOM异常。LeakCanary将在开发过程中帮助你找到并修复内存它们。当Square的工程师第一次在Square Point Of Sale应用中使用LeakCanary时,他们就可以修复一些内存泄漏同时减少94%的OOM奔溃。

Info
Your crash reporting tool might not correctly report OOMs. When memory is low because of memory leak accumulation, an OOM can be thrown from anywhere in the app code, which means that every OOM has a different stacktrace. So instead of one crash entry with a 1000 crashes, OOMs get reported as 1000 distinct crashes and hide in the long tail of low occuring crashes.

提示
也许你的异常报告工具不能正确的记录OOM异常。当(可用)内存应为内存泄漏的堆积变得很低时,在OOM异常会在应用中任意位置抛出异常,这就意味着每个内存溢出的堆栈都是不一样的。所以使用1000种异常替换到同一个入口,OOMs将会得到1000个明显不同的异常同时将隐藏低等级事故较长的尾部(这里可能是表示隐藏堆栈种较低的重复内容)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值