Home  >  Q&A  >  body text

Android中有什么好的查看代码性能的工具,类似内存泄漏,或者分析程序性能的工具?

PHP中文网PHP中文网2729 days ago707

reply all(6)I'll reply

  • 怪我咯

    怪我咯2017-04-17 16:32:34

    Fool-style MAT (Memory Analyzer Tool), even more fool-proof leakcanary

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:32:34

    Check out BlockCanary, memory leak detection library leakcannary, and MAT, etc. for app interface lags

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 16:32:34

    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.

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:32:34

    ddms, check the function execution time, traceview is also a tool

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 16:32:34

    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();
     }

    reply
    0
  • 黄舟

    黄舟2017-04-17 16:32:34

    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:

     dependencies {
       debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
       releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
     }
    

    In Application:

    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).

    reply
    0
  • Cancelreply