Android问题解决方法总结

最近项目上最近一个版本主要是解决疑难杂症问题,ANR、OOM、Crash,这些问题都有错误的堆栈信息,但是是被混淆后的。由于我们是sdk开发,所以对于开发者反馈给我们的一个混淆后的错误堆栈截图信息,一开始不知道如何下手。

大致的问题可以分为三大类:ANR、OOM、Crash.
对于SDK开发,我们对有可能异常的代码尽量的要多加try catch,虽然过多的try catch影响程序的运行,但是相比于直接让开发者的APP挂掉,后者是更致命的,所以还是要少写bug,多考虑一下。

ANR

ANR导致的根本原因就是主线程被阻塞太久,导致程序不能被响应。

  1. 对于同步锁的准确使用,避免不需要同步的耗时操作也被放入同步锁中。
  2. 尽量减少在主线程做一些耗时操作。

OOM

OOM问题涉及到Java的垃圾回收机制,判断一个对象是不是被泄露,需要看该对象到GC root之间是否可达,即存在引用树
常见的GC Root比如 存活的线程,更多请看

其中,引用又可分为 显示引用与隐式引用,显示引用 就是直接引用,而隐式引用比如内部类会隐式的持有外部类的强引用
静态内部类虽然不会直接引用外部类,但在使用过程中避免对外部类的直接引用,可以使用弱引用的方式,构造传入。

Crash

对于线上问题,解决办法:

  1. 回退到对应版本Tag分支,打包。
  2. 反编译打包生成的的 jar / apk。反编译可以使用jadx,jadx使用详细步骤
  3. 根据混淆后的错误堆栈信息,尝试从源码中找到对应代码位置。
  4. 对源码进行具体问题分析。
GC Root
1.System Class
----------Class loaded by bootstrap/system class loader. For example, everything from the rt.jar like java.util.* .
2.JNI Local
----------Local variable in native code, such as user defined JNI code or JVM internal code.
3.JNI Global
----------Global variable in native code, such as user defined JNI code or JVM internal code.
4.Thread Block
----------Object referred to from a currently active thread block.
Thread
----------A started, but not stopped, thread.
5.Busy Monitor
----------Everything that has called wait() or notify() or that is synchronized. For example, by calling synchronized(Object) or by entering a synchronized method. Static method means class, non-static method means object.
6.Java Local
----------Local variable. For example, input parameters or locally created objects of methods that are still in the stack of a thread.
7.Native Stack
----------In or out parameters in native code, such as user defined JNI code or JVM internal code. This is often the case as many methods have native parts and the objects handled as method parameters become GC roots. For example, parameters used for file/network I/O methods or reflection.
7.Finalizable
----------An object which is in a queue awaiting its finalizer to be run.
8.Unfinalized
----------An object which has a finalize method, but has not been finalized and is not yet on the finalizer queue.
9.Unreachable
----------An object which is unreachable from any other root, but has been marked as a root by MAT to retain objects which otherwise would not be included in the analysis.
10.Java Stack Frame
----------A Java stack frame, holding local variables. Only generated when the dump is parsed with the preference set to treat Java stack frames as objects.
11.Unknown
----------An object of unknown root type. Some dumps, such as IBM Portable Heap Dump files, do not have root information. For these dumps the MAT parser marks objects which are have no inbound references or are unreachable from any other root as roots of this type. This ensures that MAT retains all the objects in the dump.
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,341评论 25 709
  • 所有知识点已整理成app app下载地址 J2EE 部分: 1.Switch能否用string做参数? 在 Jav...
    侯蛋蛋_阅读 7,385评论 1 4
  • 面试必背 会舍弃、总结概括——根据我这些年面试和看面试题搜集过来的知识点汇总而来 建议根据我的写的面试应对思路中的...
    luoyangzk阅读 11,882评论 6 173
  • 我喜欢你, 不像风走了八千里,不问归期,不像雨洒落地,不远万里, 不像干旱的白杨树那般,切切企盼, 不像初春的杨柳...
    反光板阅读 1,288评论 0 0
  • 在多多少少人的心中,都曾经有一个喜欢的他(她)。都说暗恋是没有好结果的,但偏偏有人要尝试这种不可能。其结果往往让人...
    沐Summer阅读 1,805评论 0 1

友情链接更多精彩内容