Keeping a reference to a View in a Fragment causes memory leaks?

后端 未结 6 1099
难免孤独
难免孤独 2021-02-04 13:30

Somebody told me the following, but I am a bit perplexed.

Please, would you be able to confirm or dispute it?

(the Fragment is

6条回答
  •  爱一瞬间的悲伤
    2021-02-04 14:17

    From a practical point of View: The main reason for memory leaks is keeping static fields, in particular static Context, which should be avoided. Within an attached Fragment, this usually is not even required. static fields should be called .close() and set to null before super.onDetach() or super.onDestroy(). One does not even have keep handles to any views, when using data-binding. In Kotlin there are also synthetic accessors for that. Keeping handles to views is not required at all, which renders the question obsolete. This was required before both of these existed. Use lint, ktlint or leakcanary or memory-profiler to find potential memory leaks.

提交回复
热议问题