在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