未游网

leakcanary jar leakcanary jar
32位64位无插件无广告
  • 文件大小:2.9 MB
  • 界面语言:中文
  • 发布日期:2023-05-05
  • 系统平台:Win7/WinXP/Win8/Win10
  • 软件标签:编程工具
本地下载 安全放心,最新版本
广告位500x320
leakcanary是一款十分适用的内存检测工具,这款压缩文件里边涵盖了leakcanary_resource.jar和leakcanary.jar.一个是包括源码的一个是沒有包括源码的!有须要的快点来软件站免费下载!
leakcanary jar详细介绍
Android 和 java 内存泄露检验。
“A small leak will sink a great ship.” - Benjamin Franklin
溃于蚁穴, 溃于蚁穴。 《韩非子喻老》
为何必须应用 LeakCanary?
问得好,看这个文章内容LeakCanary: 让内存泄露无所遁形
怎么使用
应用 RefWatcher 监控这些本应被收购的目标。
RefWatcher refWatcher = {...}
// 监控
refWatcher.watch(schrodingerCat)
LeakCanary.install() 会回到一个预订义的 RefWatcher,与此同时也会开启一个 ActivityRefWatcher,用以全自动监控读取 Activity.onDestroy() 以后泄露的 activity。
public class ExampleApplication extends Application {
public static RefWatcher getRefWatcher(Context context) {
ExampleApplication application = (ExampleApplication) context.getApplicationContext()
return application.refWatcher
}
private RefWatcher refWatcher
@Override public void onCreate() {
super.onCreate()
refWatcher = LeakCanary.install(this)
}
}
应用 RefWatcher 监控 Fragment
public abstract class BaseFragment extends Fragment {
@Override public void onDestroy() {
super.onDestroy()
RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity())
refWatcher.watch(this)
}
}
工作方案
RefWatcher.watch() 建立一个 KeyedWeakReference 到要被监控的目标。
随后在后台管理进程查验引用是不是被消除,要是没有,读取GC。
假如引用或是未被消除,把 heap 运行内存 dump 到 APP 相匹配的操作系统中的一个 .hprof 文档中。
在此外一个过程中的 HeapAnalyzerService 有一个 HeapAnalyzer 应用HAHA 分析这一文档。
归功于唯一的 reference key, HeapAnalyzer 寻找 KeyedWeakReference,精准定位内存泄露。
HeapAnalyzer 测算 到 GC roots 的最少强引用途径,并明确是不是泄露。如果是得话,创建造成泄露的引用链。
引用链传送到 APP 过程中的 DisplayLeakService, 并以通告的方式展现出去。
如何复制 leak trace?
在 Logcat 中,你能见到相近这种的 leak trace
In com.example.leakcanary:1、0:1 com.example.leakcanary.MainActivity has leaked
* GC ROOT thread java.lang.Thread.Java Local (named AsyncTask 1)
* references com.example.leakcanary.MainActivity$3、this$0 (anonymous class extends android.os.AsyncTask)
* leaks com.example.leakcanary.MainActivity instance
* Reference Key: e71f3bf5-d786-4145-8539-584afaecad1d
* Device: Genymotion generic Google Nexus 6 - 5、1、0 - API 22 - 1440x2560 vbox86p
* Android Version: 5、1 API: 22
* Durations: watch=5086ms, gc=110ms, heap dump=435ms, analysis=2086ms
你甚至于可以根据分享按钮把这种物品共享出来。
SDK 造成的内存泄露
伴随着時间的变化,许多SDK 和生产商 ROM 中的内存泄露问题早已被尽早修补了。可是,当那样的问题出现时,一般的开发人员能做的事儿很比较有限。
LeakCanary 有一个已经知道问题的忽视目录,AndroidExcludedRefs.java,假如你发觉了一个新的问题,请提一个 issue 并另附 leak trace, reference key, 设备型号规格和 SDK 版本。假如能附加上 dump 文档的 连接那么就最好不过了。
针对最新发布的 Android,这一点特别是在关键。您有机遇在协助在初期发觉新的内存泄露,这对全部 Android 小区都是有巨大的好处。
开发设计版本的 Snapshots 包在这儿: Sonatypes snapshots repository。
leak trace 以外
有时候,leak trace 不足,你需要根据 MAT 或是 YourKit 深入分析 dump 文档。
根据下列方式,你可以寻找存在的问题
搜索全部的 com.squareup.leakcanary.KeyedWeakReference 案例。
查验 key 字段名
Find the KeyedWeakReference that has a key field equal to the reference key reported by LeakCanary.
寻找 key 和 和 logcat 导出的 key 值一样的 KeyedWeakReference。
referent 字段名相匹配的便是泄露的目标。
剩余的,便是动手能力修补了。最好查验到 GC root 的最少强引用途径逐渐。
自定UI 款式
DisplayLeakActivity 有一个默认设置的标志和标识,你只需要在你自己的 APP 資源中,更换下列資源就可。
res/
drawable-hdpi/
._leak_canary_icon.png
drawable-mdpi/
._leak_canary_icon.png
drawable-xhdpi/
._leak_canary_icon.png
drawable-xxhdpi/
._leak_canary_icon.png
drawable-xxxhdpi/
._leak_canary_icon.png
?xml version=1、0 encoding=utf-8?
resources
string name=._leak_canary_display_activity_labelMyLeaks/string
/resources
储存 leak trace
DisplayLeakActivity saves up to 7 heap dumps leak traces in the app directory. You can change that number by providing R.integer.._leak_canary_max_stored_leaks in your app
在 APP 的列表中,DisplayLeakActivity 储存了 7 个 dump 文档和 leak trace。你能在你的 APP 中,界定 R.integer.._leak_canary_max_stored_leaks 来遮盖类库的初始值。
?xml version=1、0 encoding=utf-8?
resources
integer name=._leak_canary_max_stored_leaks20/integer
/resources
提交 leak trace 到网络服务器
你能更改解决实现的默认设置个人行为,将 leak trace 和 heap dump 上传入你的网络服务器便于数据分析。
建立一个 LeakUploadService, 非常简单的便是承继 DisplayLeakService
public class LeakUploadService extends DisplayLeakService {
@Override
protected void afterDefaultHandling(HeapDump heapDump, AnalysisResult result, String leakInfo) {
if (!result.leakFound || result.excludedLeak) {
return
}
myServer.uploadLeakBlocking(heapDump.heapDumpFile, leakInfo)
}
}
请确定 release 版本 应用 RefWatcher.DISABLED
public class ExampleApplication extends Application {
public static RefWatcher getRefWatcher(Context context) {
ExampleApplication application = (ExampleApplication) context.getApplicationContext()
return application.refWatcher
}
private RefWatcher refWatcher
@Override public void onCreate() {
super.onCreate()
refWatcher = installLeakCanary()
}
protected RefWatcher installLeakCanary() {
return RefWatcher.DISABLED
}
}
自定 RefWatcher
public class DebugExampleApplication extends ExampleApplication {
protected RefWatcher installLeakCanary() {
return LeakCanary.install(app, LeakUploadService.class)
}
}
别忘记申请注册 service
?xml version=1、0 encoding=utf-8?
manifest xmlns:android=http://schemas.android.com/apk/res/android
xmlns:tools=http://schemas.android.com/tools
application android:name=com.example.DebugExampleApplication
service android:name=com.example.LeakUploadService /
/application
/manifest