问题描述
我看到很多 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 中的垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!