Effective Kotlin: Item 24 — Favor static member classes over nonstatic
With item 24, Joshua Bloch in Effective Java talks about preferring static member classes over non-static member classes which have a hidden reference to the enclosing instance that can prevent garbage collection (item 7).
On Android, non-static member classes and anonymous classes have been the cause of many memory leaks as, for example, the code below shows an
AsyncTask that implicitly holds a reference to the
Activity until the long-running task completes:
public class LeakyActivity…