For learning purposes I am currently experimenting with Garbage Collection (Pixel 2, Android 10 -> ART). My observation for the following experiment is that CountActivity is never deleted on the first explicit garbage collection run, but only at the second explicit garbage collection run. I want to understand why it isn't deleted on the first run. I assume it's due to some kind of generational garbage collection, but I want to verify it. How can I "watch" what the garbage collection does? E.g. is it possible to see the internal generations in which the garbage collection groups the heap?
My test example is as follows:
MainActivity, which shows a Button "Show CountActivity"CountActivitywhich is shown when the Button "Show CountActivity" in theMainActivityis pressed.- I end the
CountActivityusing my system back key
After this, I force an explicit garbage collection run, capture a heap. CountActivityis still there. After forcing a second explicit garbage collection run and again capturing a heap, the CountActivity is gone. Now I still want to understand why it takes two garbage collection runs.
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buttonStart.setOnClickListener {
showCountActivity()
}
}
private fun showCountActivity() {
val intent = Intent(this, CountActivity::class.java)
startActivity(intent)
}
}
CountActivity
class CountActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_count)
}
}