gpt4 book ai didi

android - android 有垃圾收集功能吗?它有多快?

转载 作者:行者123 更新时间:2023-11-30 00:35:04 24 4
gpt4 key购买 nike

Android 是否有垃圾回收?它是有效的还是应该在不需要时将对象设置为 null

例如,我有一个ImageView iv。我用

设置图像位图

iv.setImageBitmap(some_function_that_returns_a_bitmap_that_is_newly_created())

一段时间后我运行相同的函数

iv.setImageBitmap(some_function_that_returns_a_bitmap_that_is_newly_created())

第一个函数 some_function_that_returns_a_bitmap_that_is_newly_created() 创建的位图会发生什么变化?垃圾收集会删除它吗?这样做有什么不同吗

Bitmap temp =some_function_that_returns_a_bitmap_that_is_newly_created();
iv.setImageBitmap(temp);
temp = null;
...
temp =some_function_that_returns_a_bitmap_that_is_newly_created();

代替?

这样做有什么不同吗

Bitmap temp =some_function_that_returns_a_bitmap_that_is_newly_created();
...
temp = null;
...
temp =some_function_that_returns_a_bitmap_that_is_newly_created();

而不是这个

Bitmap temp = some_function_that_returns_a_bitmap_that_is_newly_created();
...
temp = some_function_that_returns_a_bitmap_that_is_newly_created();

?

最佳答案

是的,Android 的 VM 会进行垃圾回收。在非常非常少的地方,使对对象的引用为空是节省空间的有效方法……但您已经找到了其中一个。

位图很大,毫无疑问,它有助于将对您不再需要的位图的引用置空。

在第二个示例中显示的代码中,您的直觉是正确的,对旧位图的引用将阻止它被回收,直到将对新位图的引用分配给 temp。这意味着在某个时候,两者都在内存中。

您的代码的第一个版本没有这个问题。

编辑添加: 顺便说一句,Java 中的标准做法是驼峰式,而不是 under_scoring ;-P

关于android - android 有垃圾收集功能吗?它有多快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com