If it freezes and exits, you still need to check whether the Log is a crash or ANR. If it crashes, look at the backtrace to fix it. If it is ANR, it is mainly to put the longer actions executed in the main thread into the child thread for execution.
The main problem of the poster is that he doesn’t know where time-consuming operations are used in the main thread. If so, turning on StrictMode will undoubtedly help you quickly find the code that needs attention. StrictMode, strict mode, is provided by Android A runtime detection mechanism used to detect some irregular operations when the code is running. The most common scenario is to discover the IO operations of the main thread. StrictMode contains the concept of two dimensions: Policy (policy): refers to StrictMode’s discovery strategy for some illegal operations, which is divided into two categories: one is for a specific thread (ThreadPolicy), and the other is for All objects of virtual machines (VMPolicy). Penalty: refers to the way StrictMode punishes illegal operations after discovering them, such as drawing red boxes, printing logs, displaying dialog boxes, killing processes, etc. Android has StrictMode embedded in many key code paths, such as disk reading and writing, network access, system process startup, etc. StrictMode will check according to the set policy. If a process commits a violation while the code is running, it will be "punished". For example, in the following code, if there is a violation, the location of the illegal operation and the call stack information will be printed in logcat. onCreate of android.app.Application turns on StickMode:
public void onCreate() {
if (DEVELOPER_MODE) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
}
super.onCreate();
}
1. The client recommends LeakCanary, OOM can be self-tested and you can solve it in advance without waiting for the test to raise bugs. Add references to build.gradle. Different compilations use different references:
public class ExampleApplication extends Application {
@Override public void onCreate() {
super.onCreate();
LeakCanary.install(this);
}
}
2. You can also use the Dump Java Heap in Android Studio to export the hprof file, use the "hprof-conv xxxxx.hprof yyyyy.hprof" command to convert it and analyze it in MAT, (where xxxxx.hprof is the original file, yyyyy.hprof for the converted file).
Fool-style MAT (Memory Analyzer Tool), even more fool-proof leakcanary
Check out BlockCanary, memory leak detection library leakcannary, and MAT, etc. for app interface lags
If it freezes and exits, you still need to check whether the Log is a crash or ANR. If it crashes, look at the backtrace to fix it. If it is ANR, it is mainly to put the longer actions executed in the main thread into the child thread for execution.
ddms, check the function execution time, traceview is also a tool
The main problem of the poster is that he doesn’t know where time-consuming operations are used in the main thread. If so, turning on StrictMode will undoubtedly help you quickly find the code that needs attention.
StrictMode, strict mode, is provided by Android A runtime detection mechanism used to detect some irregular operations when the code is running. The most common scenario is to discover the IO operations of the main thread.
StrictMode contains the concept of two dimensions:
Policy (policy): refers to StrictMode’s discovery strategy for some illegal operations, which is divided into two categories: one is for a specific thread (ThreadPolicy), and the other is for All objects of virtual machines (VMPolicy).
Penalty: refers to the way StrictMode punishes illegal operations after discovering them, such as drawing red boxes, printing logs, displaying dialog boxes, killing processes, etc.
Android has StrictMode embedded in many key code paths, such as disk reading and writing, network access, system
process startup, etc. StrictMode will check according to the set policy. If a process commits a violation while the code is running, it will be "punished".
For example, in the following code, if there is a violation, the location of the illegal operation and the call stack information will be printed in logcat.
onCreate of android.app.Application turns on StickMode:
1. The client recommends LeakCanary, OOM can be self-tested and you can solve it in advance without waiting for the test to raise bugs.
Add references to build.gradle. Different compilations use different references:
In Application:
2. You can also use the Dump Java Heap in Android Studio to export the hprof file, use the "hprof-conv xxxxx.hprof yyyyy.hprof" command to convert it and analyze it in MAT, (where xxxxx.hprof is the original file, yyyyy.hprof for the converted file).