Last active
December 9, 2022 17:49
-
-
Save Mercandj/5d77de0e521c63131c33b64853df776d to your computer and use it in GitHub Desktop.
Memory leak medium article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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