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.

你可能感兴趣的:(Android问题解决方法总结)