问一个安卓开发相关的问题

2019-11-12 10:12:04 +08:00
 Vegetable

我不是安卓开发,但是有些好奇这个机制↓:

在抖音里边,我从某一个账号开始,查看他的粉丝,选中一个粉丝继续查看这个选中粉丝的粉丝.

这样我可以一直打开新的页面,点击返回的话,会回到上一个页面,那这些历史记录是怎么处理的呢?

如此往复下去可以打开足够多的个人详情页面导致 app 崩溃吗? 还是说那些页面会被回收掉,只保存了一个路径而已?

7420 次点击
所在节点    Android
29 条回复
vigidroid
2019-11-12 20:13:10 +08:00
@BigDogWang 没记错的话,android 中的内存回收是以进程为单位的。一般情况下,activity 栈中的所有 activity 都处于同一进程中,所以会内存会一直累积直到 oom
ukyoo
2019-11-12 20:20:41 +08:00
自己新建个 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 所在的进程。
yukiww233
2019-11-12 20:25:19 +08:00
@ukyoo #22 感谢。差点信了楼上说的,虽然自己做的应用都预防了循环路由
BigDogWang
2019-11-13 09:40:31 +08:00
@vigidroid 你到底是不是安卓开发😂
BigDogWang
2019-11-13 09:44:44 +08:00
@fhvch Activity 的 onSaveInstanceState 就是为了这种场景设计的。不过楼上有人做实验,崩了
BigDogWang
2019-11-13 09:49:47 +08:00
A background activity (an activity that is not visible to the user and has been stopped) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

https://developer.android.com/reference/android/app/Activity#ProcessLifecycle
官方文档
BigDogWang
2019-11-13 09:52:27 +08:00
@BigDogWang 淦,收回我的话,这个还真是杀进程的。看来对安卓的基础部分还存在误解
qiibeta
2019-11-13 11:37:17 +08:00
Activity 会直接内存一直增加直到 OOM,https://github.com/bytedance/scene 现在也是这样……想做 [超过多少个页面就把之前老页面销毁,等返回老页面的时候再恢复,来节省内存] 这样的功能,还没做
imn1
2019-11-13 14:24:49 +08:00
昨天帮老妈更新一下手机 app,打开 chrome,竟然看到 98 个 tab……98 个……呆了几秒
比我桌面系统还狠

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/618686

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX