自己新建个 Activity, 在 onCreate 里分配个大内存的数组, 无限重复开页面.
class DetailActivity1111 : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.acti111)
val ints = IntArray(120000000)
findViewById<Button>(R.id.btn).setOnClickListener {
startActivity(Intent(this, DetailActivity1111::class.java))
}
}
}
最后崩溃的日志如下:
JNI DETECTED ERROR IN APPLICATION: JNI IsInstanceOf called with pending exception java.lang.OutOfMemoryError: Failed to allocate a 480000016 byte allocation with 25165824 free bytes and 457MB until OOM, max allowed footprint 82083784, growth limit 536870912
代码就两三行,不存在内存泄露. 所以回收栈底 Activity 来回收内存的说法不成立.
再看官方文档:
https://developer.android.com/guide/components/activities/activity-lifecycle#asem写的很明白: 系统永远不会直接终止 Activity 以释放内存,而是会终止 Activity 所在的进程。