最近項目上最近一個版本主要是解決疑難雜癥問題,ANR、OOM、Crash,這些問題都有錯誤的堆棧信息,但是是被混淆后的。由于我們是sdk開發(fā),所以對于開發(fā)者反饋給我們的一個混淆后的錯誤堆棧截圖信息,一開始不知道如何下手。
大致的問題可以分為三大類:ANR、OOM、Crash.
對于SDK開發(fā),我們對有可能異常的代碼盡量的要多加try catch,雖然過多的try catch影響程序的運行,但是相比于直接讓開發(fā)者的APP掛掉,后者是更致命的,所以還是要少寫bug,多考慮一下。
ANR
ANR導(dǎo)致的根本原因就是主線程被阻塞太久,導(dǎo)致程序不能被響應(yīng)。
- 對于同步鎖的準確使用,避免不需要同步的耗時操作也被放入同步鎖中。
- 盡量減少在主線程做一些耗時操作。
OOM
OOM問題涉及到Java的垃圾回收機制,判斷一個對象是不是被泄露,需要看該對象到GC root之間是否可達,即存在引用樹。
常見的GC Root比如 存活的線程,更多請看。
其中,引用又可分為 顯示引用與隱式引用,顯示引用 就是直接引用,而隱式引用比如內(nèi)部類會隱式的持有外部類的強引用。
靜態(tài)內(nèi)部類雖然不會直接引用外部類,但在使用過程中避免對外部類的直接引用,可以使用弱引用的方式,構(gòu)造傳入。
Crash
對于線上問題,解決辦法:
- 回退到對應(yīng)版本Tag分支,打包。
- 反編譯打包生成的的 jar / apk。反編譯可以使用jadx,jadx使用詳細步驟。
- 根據(jù)混淆后的錯誤堆棧信息,嘗試從源碼中找到對應(yīng)代碼位置。
- 對源碼進行具體問題分析。
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.