Garbage collector in Android(Android 中的垃圾收集器)
问题描述
我看到很多 Android 回答建议在某些情况下调用垃圾收集器.
I have seen many Android answers that suggest calling the garbage collector in some situations.
在执行需要大量内存的操作之前请求 Android 中的垃圾收集器是一种好习惯吗?如果没有,我是否应该只在收到 OutOfMemory
错误时才调用它?
Is it a good practice to request the garbage collector in Android before doing a memory-hungry operation? If not, should I only call it if I get an OutOfMemory
error?
在求助于垃圾收集器之前我还应该使用其他东西吗?
Are there other things I should use before resorting to the garbage collector?
推荐答案
对于 3.0 honeycomb 之前的版本:是的,调用 System.gc()
.
我尝试创建位图,但总是收到VM 内存不足错误".但是,当我先调用 System.gc()
时,就可以了.
I tried to create Bitmaps, but was always getting "VM out of memory error". But, when I called System.gc()
first, it was OK.
在创建位图时,Android 经常会因内存不足错误而失败,并且不会首先尝试进行垃圾收集.因此,调用 System.gc()
,您就有足够的内存来创建位图.
When creating bitmaps, Android often fails with out of memory errors, and does not try to garbage collect first. Hence, call System.gc()
, and you have enough memory to create Bitmaps.
如果创建对象,我认为 System.gc
会在需要时自动调用,但 不是 用于创建位图.它只是失败了.
If creating Objects, I think System.gc
will be called automatically if needed,
but not for creating bitmaps. It just fails.
所以我建议在创建位图之前手动调用 System.gc()
.
So I recommend manually calling System.gc()
before creating bitmaps.
这篇关于Android 中的垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 中的垃圾收集器


- 如何使用WebFilter实现授权头检查 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01