Skip to content

Instantly share code, notes, and snippets.

@Mercandj
Last active December 9, 2022 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mercandj/5d77de0e521c63131c33b64853df776d to your computer and use it in GitHub Desktop.
Save Mercandj/5d77de0e521c63131c33b64853df776d to your computer and use it in GitHub Desktop.
Memory leak medium article
class MainActivity : AppCompatActivity() {
private val listener = Manager.Listener {
Log.d("memory_leak", "Use largeObject to avoid compiler opti $largeObject")
}
private var largeObject = (0..4_000).map {
Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
manager.listeners.add(listener) // Leak without onDestroy() remove
}
companion object {
@JvmStatic
private val manager = Manager()
private class Manager {
val listeners = HashSet<Listener>()
fun interface Listener { fun onChanged() }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment