美文网首页
LeakCanary 学习笔记

LeakCanary 学习笔记

作者: 墨奕仙起 | 来源:发表于2018-07-31 12:49 被阅读0次

    github 地址

    添加依赖

        debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.1'
        releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.1'
        // Optional, if you use support library fragments:
        debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.1'
    

    创建自定义的Application

    public class ExampleApplication extends Application {
    
        @Override public void onCreate() {
            super.onCreate();
            if (LeakCanary.isInAnalyzerProcess(this)) {
                // This process is dedicated to LeakCanary for heap analysis.
                // You should not init your app in this process.
                return;
            }
            LeakCanary.install(this);
            // Normal app init code...
        }
    }
    

    在AndroidManifest.xml的appication标签中添加如下

    android:name=".ExampleApplication"
    

    测试

    • 创建一个secondActivity
    public class secondActivity extends AppCompatActivity {
    
        static Demo demo;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            if(demo==null)
            {
                demo=new Demo();
            }
    
            finish();
        }
    
        class Demo
        {
    
        }
    }
    
    • MainActivity
    public class MainActivity extends AppCompatActivity {
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent intent=new Intent(MainActivity.this,secondActivity.class);
            startActivity(intent);
    
        }
    
    
    }
    

    结果

    image.png Screenshot_1533012508.png

    相关文章

      网友评论

          本文标题:LeakCanary 学习笔记

          本文链接:https://www.haomeiwen.com/subject/jlzxvftx.html