在Lunar Lander示例中(由Google的Android站点提供),在名为“run()”的方法中有一个主循环。这是如下:Lunar Lander中的Android垃圾收集
@Override
public void run() {
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) updatePhysics();
doDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
所以,现在,我的问题是这样的:保持零循环,因此导致更多的垃圾收集是不是很糟糕?也许我不太了解这一点,但为什么它一定是空的?
循环中对象的重新创建不会导致垃圾回收? – Eugene 2010-06-29 18:36:01
@Eugene - 如果你正在实例化和分配内存给对象,那么是的。您将强制定期进行垃圾回收以释放该内存。 Android站点在你的应用程序中调用这些'打嗝'。但是将null分配给对象并不是一回事。 – JonH 2010-06-29 18:42:24
好吧,我想我明白每个人的答案。感谢您的帮助! – Eugene 2010-06-29 18:45:28