Android uses LeakCanary to detect memory overflow

// some common memory overflows
Reprinted:  https://www.cnblogs.com/whoislcj/p/6001422.html

 https://blog.csdn.net/u014005316/article/details/63258107 
 

 
 
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'

releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'

public class LeakApplication extends Application {
    private RefWatcher refWatcher;

    @Override
public void onCreate() {
        super.onCreate();
        refWatcher = setupLeakCanary();    
    }

    private RefWatcher setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return RefWatcher.DISABLED;
        }
        return LeakCanary.install(this);
    }

    public static RefWatcher getRefWatcher(Context context) {
        LeakApplication leakApplication = (LeakApplication) context.getApplicationContext();
        return leakApplication.refWatcher;
    }
}


public class MainActivity extends AppCompatActivity {
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);
        LeakThread leakThread = new LeakThread();
        leakThread.start();
    }
    class LeakThread extends Thread {
        @Override
public void run() {
            try {        
                Thread.sleep(6 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
        }
    }
    @Override
protected void onDestroy() {
        super.onDestroy();    
        RefWatcher refWatcher = LeakApplication.getRefWatcher(this);//1
        refWatcher.watch(this);
    }
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986878&siteId=291194637