android6-6 Exercise 2: A leak and a heap dump ========================================== // A classic leak: a long-lived object holds a short-lived one object NotesCache { var screenCallback: (() -> Unit)? = null // a global (long-lived) variable... } @Composable fun NotesScreen() { val context = LocalContext.current // an Activity NotesCache.screenCallback = { Toast.makeText(context, "Hi", Toast.LENGTH_SHORT).show() } // ...the lambda captures the Activity, and the object lives as long as the app does: // the Activity cannot be garbage-collected after rotation or finish(). } // The fix: do not park UI references in long-lived objects; clear them when the screen is // disposed, e.g. DisposableEffect(Unit) { onDispose { NotesCache.screenCallback = null } } Steps: add the leaky code, run, rotate the screen 3-4 times, in Android Studio open the Profiler, record memory, use the Heap Dump action (docs), filter by your Activity class and count instances. Expect more than one after rotations (general knowledge). Apply the DisposableEffect fix, repeat, and report both counts. VmPolicy.detectActivityLeaks() may also log it (docs). Code unrun.