Memory leak in the empty Activity

北城余情 提交于 2021-01-27 12:45:56

问题


I recently decided to use leakcanary in my projects, So I created a project with an empty Activity just for test, When I run the app (just after project creation with no logic code or views) I got memory leak log from this library:

07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * com.example.leaktest.MainActivity has leaked:
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * GC ROOT static android.app.ActivityThread.sCurrentActivityThread
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * references android.app.ActivityThread.mActivities
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * references android.util.ArrayMap.mArray
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * references array java.lang.Object[].[1]
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * references android.app.ActivityThread$ActivityClientRecord.activity
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * leaks com.example.leaktest.MainActivity instance
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * Retaining: 1.7KB.
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * Reference Key: 9180226a-8a65-4c94-9d12-4562a6d88157
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * Device: Genymotion generic genymotion_vbox86tp_5.1_150409_105318 vbox86tp
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * Android Version: 5.1 API: 22 LeakCanary: 1.4-beta2 3799172
07-20 04:32:36.742 2967-4915/com.example.leaktest D/LeakCanary: * Durations: watch=5808ms, gc=158ms, heap dump=1953ms, analysis=15795ms

App class:

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        refWatcher = LeakCanary.install(this);
    }

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

    private RefWatcher refWatcher;
}

MainActivity class:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        App.getRefWatcher(this).watch(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

I have no idea why this is happening, I will appreciate if someone explain it to me.


回答1:


Your RefWatcher should be in the onDestroy() method, not onCreate() (see a similar reported issue here).

You don't even need to do this, since LeakCanary watches Activity references automatically. From the FAQ:

LeakCanary.install() returns a pre configured RefWatcher. It also installs an ActivityRefWatcher that automatically detects if an activity is leaking after Activity.onDestroy() has been called.



来源:https://stackoverflow.com/questions/38470388/memory-leak-in-the-empty-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!