leakcanary

Android进阶7:内存优化——LeakCanary原理分析

烈酒焚心 提交于 2019-12-03 16:31:05
好的项目离不开健壮的代码,对于想要写出健壮的代码,解决内存泄漏是必须的。 对于LeakCanary,对于大多人是不陌生的,也就是检测内存泄漏的工具。可能在代码中我们会这样引入LeakCanary: //检查leakCanary和APP是否在同一个进程,如果是同一个进程就返回,不在同一个进程,就注册。 //因为不再同一个进程,不会对APP进程造成消极影响(如:APP进程变慢或者out of memory) if (LeakCanary.isInAnalyzerProcess(this)) { // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. return; } LeakCanary.install(this); 简简单单的两句话,背后的原理却是一大堆。 在开始源码之前,先说几个知识点: 强引用,软引用,弱引用,GC线程扫描它所管辖的内存区域时,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。 垃圾回收器是一个优先级很低的线程,即使有弱引用的存在,也不一定会执行。 我们手动调用GC,不一定成功调用垃圾回收器,因为我们仅仅是建议JVM执行GC,最终执不执行,还是得看JVM的最终决策。

How to treat memory leaks using the new AndroidProfiler

混江龙づ霸主 提交于 2019-12-03 13:13:07
I'm trying to learn how to identify and treat memory leaks in my App. I'm reading this great article , but I'm a bit confused about how to apply that in the new "Android Profiler" tool, that came with Android Studio 3.0. I'have a test application and LeakCanary is pointing that my App has memory leaks. In fact, when I run the profiler and press "dump java heap" I can see that there are 4 instances of my MyCollectionActivity and, when I click on these instances, I can see the details: But the tool that is shown in the article has options I cannot see in the "Android Profiler": I will not go

Stuck at “Dumping memory, app will freeze. Brrr.” message

亡梦爱人 提交于 2019-12-03 09:27:05
I'm trying to use LeakCanary to detect memory leaks in my app, but it does not go further than the message "Dumping memory, app will freeze. Brrr." I've been waiting for about 20 minutes or so, but no changes. Same behaviour on these devices: 1. Asus fonepad 8 (Android 5.0 stock) 2. Sony Xperia SP (Android 5.1.1 CM 12.1 custom) 3. HTC Desire C (Android 4.4 CM 11 custom) I did everything as its advised in instruction: public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); LeakCanary.install(this); } } If you're on Android M you need to grant

Uncaught translation error: com.android.dx.cf.code.SimException: local 0007: invalid

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I don't use Proguard. The project is building successfully but when I try to run on the device it shows this error. I was working in Android Studio 3.0 with a Kotlin project and it was really bad experience. I decided to switch back to 2.3 AS but the problem is still the same. The log is smaller but the exception text is the same. apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' tasks.withType(JavaCompile) { options.fork = true options.incremental =

Android memory leak on textview - LeakCanary (Leak can be ignored)

余生长醉 提交于 2019-12-03 02:59:53
I keep having the following memory leak as displayed by LeakCanary, when I go from my splash screen to the mainactivity. I understand that this is an expected leak due to fault in the Android OS itself, but is there a way I can avoid this (by setting specifics of some TextView somewhere?) D/LeakCanary﹕ * LEAK CAN BE IGNORED. D/LeakCanary﹕ * com.gmspartnersltd.earthmiles.views.ActivitySignUp_ has leaked: D/LeakCanary﹕ * GC ROOT static android.text.TextLine.sCached D/LeakCanary﹕ * references array android.text.TextLine[].[1] D/LeakCanary﹕ * references android.text.TextLine.mCharacterStyleSpanSet

More than one file was found with OS independent path 'META-INF/LICENSE'

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When i build my app, I got follow error . Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. More than one file was found with OS independent path 'META-INF/LICENSE' This is My build.gradle file apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "cn.sz.cyrus.kotlintest" minSdkVersion 14 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner

APP内存泄露问题的解决过程

匿名 (未验证) 提交于 2019-12-03 00:39:02
一、如何发现内存泄露了 1.打开android studio,运行APP,android studio底部栏选择 “Android Monitor”的“Monitors”视图 2.在Monitors界面的上部分,左边下拉框选择运行APP的手机或模拟器,右边下拉框选择要调试的APP进程。 3.在Monitors界面的中间部分重点关注“Memory”这一块的内存值的变化。 此时Activity正常情况下应该会被回收,已分配内存值“Allocated”应该会恢复成打开之前的值。 4.生成hprof文件进行验证与分析 点击“Dump java Heap”生成hprof文件 大概5秒后,hprof文件会被自动生成,并自动显示在代码浏览区域 此视图会显示对象的类型与实例个数,我们可以按包名进行分类,这样更方便查找自己定义的类 二、通过hprof文件分析内存泄露 用Package Tree View分类,能很快找到我们需要分析的Activity (本APP的 launcher Activity点击进去,第二级的Activity名为MainActivity,当按返回按钮后,MainActivity正常情况下要被回收,我们正是分析MainActivity为什么发生内存泄露) 我在launcher Activity里点击进MainActivity 4次并返回,通过上图可以发现,回到launcher

Android内存优化:LeakCanary使用详解

匿名 (未验证) 提交于 2019-12-03 00:22:01
1.概述 如果使用MAT来分析内存问题,会有一些难度,并且效率也不是很高,对于一个内存泄漏问题,可能要进行多次排查和对比。 为了能够简单迅速的发现内存泄漏,Square公司基于MAT开源了 LeakCanary 。 2.使用LeakCanary 首先配置build.gradle: dependencies { debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.2' releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.2' } 1 2 3 4 接下来在Application加入如下代码。 public class LeakApplication extends Application { @Override public void onCreate () { super .onCreate(); if (LeakCanary.isInAnalyzerProcess( this )) { //1 // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. return ;

LeakCanary 与 鹅场Matrix ResourceCanary对比分析

匿名 (未验证) 提交于 2019-12-02 23:47:01
推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) 1、组件启动 LeakCanary自动注册启动 原理:专门定制了一个ContentProvider,来注册启动LeakCanary 实现如下: /** * Content providers are loaded before the application class is created. [LeakSentryInstaller] is * used to install [leaksentry.LeakSentry] on application start. */ internal class LeakSentryInstaller : ContentProvider() { override fun onCreate(): Boolean { CanaryLog.logger = DefaultCanaryLog() val application = context!!.applicationContext as Application InternalLeakSentry.install(application) return true } ... } public class MatrixApplication extends

How to use Leak Canary

邮差的信 提交于 2019-12-02 18:54:28
I know this is probably a dumb question, but I am pretty new at developing android, and I currently experiencing an OutOfMemoryError in my apps, which I have tried to debug using MAT, but it is still too hard to find the leak in a few activities, then I found LeakCanary, which seems simpler and easier to use, however I could not find any beginner step by step guide on using Leak Canary, even on Google. I have installed LeakCanary through the dependencies in my build.gradle, and this is what I got so far : ExampleApplication.java public class ExampleApplication extends Application { public