下面是我的游戏循环和 logCat 输出。游戏运行有点不稳定,我在 LogCat 中注意到 GC 经常运行。我不太确定是 GC 还是 delta 时间导致了这种情况?有人可以根据提供的信息为我指出正确的方向吗?
@Override
public void run() {
Canvas canvas;
while (isRunning) {
if (!gamePanel.paused) {
canvas = null;
try {
startDraw = System.currentTimeMillis();
canvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
gamePanel.update(deltaTime);
gamePanel.draw(canvas);
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
endDraw = System.currentTimeMillis();
MyLogger.getInstance().log(
"delta time = " + (endDraw - startDraw)
/ 1000.f);
}
}
deltaTime = (endDraw - startDraw)
/ 1000.f;
}
}
}
-09-04 15:59:29.855: D/dalvikvm(3273): GC_FOR_ALLOC freed 4427K, 35% free 18542K/28387K, paused 25ms, total 27ms
09-04 15:59:29.855: I/dalvikvm-heap(3273): Grow heap (frag case) to 22.083MB for 4096016-byte allocation
09-04 15:59:29.886: D/dalvikvm(3273): GC_CONCURRENT freed 2278K, 29% free 20263K/28387K, paused 3ms+3ms, total 27ms
09-04 15:59:30.144: D/MediaPlayer(3273): start() mUri is null
09-04 15:59:30.160: D/MyLogger(3273): delta time = 0.026
09-04 15:59:30.214: D/MyLogger(3273): delta time = 0.052
09-04 15:59:30.253: D/MyLogger(3273): delta time = 0.042
09-04 15:59:30.277: D/MyLogger(3273): delta time = 0.019
09-04 15:59:30.293: D/MyLogger(3273): delta time = 0.015
09-04 15:59:30.308: D/MyLogger(3273): delta time = 0.017
09-04 15:59:30.324: D/MyLogger(3273): delta time = 0.016
09-04 15:59:30.339: D/MyLogger(3273): delta time = 0.016
09-04 15:59:30.355: D/MyLogger(3273): delta time = 0.016
09-04 15:59:30.386: D/MyLogger(3273): delta time = 0.026
09-04 15:59:30.402: D/MyLogger(3273): delta time = 0.018
09-04 15:59:30.418: D/MyLogger(3273): delta time = 0.019
09-04 15:59:30.441: D/MyLogger(3273): delta time = 0.018
09-04 15:59:30.457: D/MyLogger(3273): delta time = 0.015
09-04 15:59:30.464: D/MyLogger(3273): delta time = 0.012
09-04 15:59:30.480: D/MyLogger(3273): delta time = 0.018
09-04 15:59:30.496: D/MyLogger(3273): delta time = 0.014
09-04 15:59:30.519: D/MyLogger(3273): delta time = 0.017
09-04 15:59:30.535: D/MyLogger(3273): delta time = 0.017
09-04 15:59:30.550: D/MyLogger(3273): delta time = 0.017
09-04 15:59:30.574: D/MyLogger(3273): delta time = 0.02
09-04 15:59:30.589: D/MyLogger(3273): delta time = 0.017
09-04 15:59:30.605: D/MyLogger(3273): delta time = 0.016
09-04 15:59:30.621: D/MyLogger(3273): delta time = 0.02
09-04 15:59:30.644: D/MyLogger(3273): delta time = 0.018
09-04 15:59:32.550: D/MyLogger(3273): delta time = 0.016
09-04 15:59:32.566: D/MyLogger(3273): delta time = 0.017
09-04 15:59:32.582: D/MyLogger(3273): delta time = 0.015
09-04 15:59:32.597: D/MyLogger(3273): delta time = 0.018
09-04 15:59:32.621: D/MyLogger(3273): delta time = 0.019
09-04 15:59:32.636: D/MyLogger(3273): delta time = 0.014
09-04 15:59:32.652: D/MyLogger(3273): delta time = 0.018
09-04 15:59:32.668: D/MyLogger(3273): delta time = 0.015
09-04 15:59:32.683: D/MyLogger(3273): delta time = 0.017
09-04 15:59:32.699: D/MyLogger(3273): delta time = 0.017
09-04 15:59:32.714: D/MyLogger(3273): delta time = 0.017
09-04 15:59:32.730: D/MyLogger(3273): delta time = 0.016
09-04 15:59:32.753: D/MyLogger(3273): delta time = 0.017
09-04 15:59:32.769: D/MyLogger(3273): delta time = 0.016
09-04 15:59:32.785: D/MyLogger(3273): delta time = 0.016
09-04 15:59:32.808: D/MyLogger(3273): delta time = 0.021
09-04 15:59:36.535: D/MyLogger(3273): delta time = 0.012
09-04 15:59:36.558: D/MyLogger(3273): delta time = 0.019
09-04 15:59:36.574: D/MyLogger(3273): delta time = 0.017
09-04 15:59:36.589: D/MyLogger(3273): delta time = 0.018
09-04 15:59:36.605: D/MyLogger(3273): delta time = 0.015
09-04 15:59:36.621: D/MyLogger(3273): delta time = 0.016
09-04 15:59:36.644: D/MyLogger(3273): delta time = 0.018
09-04 15:59:36.652: D/MyLogger(3273): delta time = 0.014
09-04 15:59:36.675: D/MyLogger(3273): delta time = 0.02
09-04 15:59:36.691: D/MyLogger(3273): delta time = 0.016
09-04 15:59:36.707: D/MyLogger(3273): delta time = 0.015
09-04 15:59:36.722: D/MyLogger(3273): delta time = 0.016
09-04 15:59:38.214: D/MyLogger(3273): delta time = 0.015
09-04 15:59:38.230: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.246: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.269: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.285: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.308: D/MyLogger(3273): delta time = 0.022
09-04 15:59:38.332: D/MyLogger(3273): delta time = 0.021
09-04 15:59:38.347: D/MyLogger(3273): delta time = 0.016
09-04 15:59:38.363: D/MyLogger(3273): delta time = 0.016
09-04 15:59:38.378: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.402: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.418: D/MyLogger(3273): delta time = 0.02
09-04 15:59:38.433: D/MyLogger(3273): delta time = 0.014
09-04 15:59:38.457: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.472: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.488: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.503: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.519: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.535: D/MyLogger(3273): delta time = 0.016
09-04 15:59:38.550: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.574: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.589: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.636: D/dalvikvm(3273): GC_CONCURRENT freed 5659K, 35% free 18532K/28387K, paused 13ms+2ms, total 42ms
09-04 15:59:38.636: D/MyLogger(3273): delta time = 0.043
09-04 15:59:38.652: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.668: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.683: D/MyLogger(3273): delta time = 0.013
09-04 15:59:38.699: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.730: D/MyLogger(3273): delta time = 0.025
09-04 15:59:38.746: D/MyLogger(3273): delta time = 0.02
09-04 15:59:38.769: D/MyLogger(3273): delta time = 0.02
09-04 15:59:38.785: D/MyLogger(3273): delta time = 0.014
09-04 15:59:38.808: D/MyLogger(3273): delta time = 0.022
09-04 15:59:38.824: D/MyLogger(3273): delta time = 0.022
09-04 15:59:38.839: D/MyLogger(3273): delta time = 0.017
09-04 15:59:38.863: D/MyLogger(3273): delta time = 0.02
09-04 15:59:38.878: D/MyLogger(3273): delta time = 0.016
09-04 15:59:38.894: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.918: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.933: D/MyLogger(3273): delta time = 0.018
09-04 15:59:38.957: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.972: D/MyLogger(3273): delta time = 0.019
09-04 15:59:38.996: D/MyLogger(3273): delta time = 0.023
09-04 15:59:39.011: D/MyLogger(3273): delta time = 0.02
09-04 15:59:39.035: D/MyLogger(3273): delta time = 0.018
09-04 15:59:39.050: D/MyLogger(3273): delta time = 0.018
09-04 15:59:39.074: D/MyLogger(3273): delta time = 0.019
09-04 15:59:39.089: D/MyLogger(3273): delta time = 0.018
09-04 15:59:39.113: D/MyLogger(3273): delta time = 0.022
09-04 15:59:39.128: D/MyLogger(3273): delta time = 0.017
09-04 15:59:39.144: D/MyLogger(3273): delta time = 0.017
09-04 15:59:39.160: D/MyLogger(3273): delta time = 0.018
09-04 15:59:39.191: D/MyLogger(3273): delta time = 0.029
09-04 15:59:39.222: D/MyLogger(3273): delta time = 0.028
09-04 15:59:39.238: D/MyLogger(3273): delta time = 0.017
09-04 15:59:39.261: D/MyLogger(3273): delta time = 0.02
09-04 15:59:39.277: D/MyLogger(3273): delta time = 0.016
09-04 15:59:39.293: D/MyLogger(3273): delta time = 0.017
09-04 15:59:39.308: D/MyLogger(3273): delta time = 0.019
09-04 15:59:39.324: D/MyLogger(3273): delta time = 0.014
09-04 15:59:39.339: D/MyLogger(3273): delta time = 0.015
09-04 15:59:39.355: D/MyLogger(3273): delta time = 0.018
09-04 15:59:39.371: D/MyLogger(3273): delta time = 0.016
09-04 15:59:39.394: D/MyLogger(3273): delta time = 0.019
09-04 15:59:39.410: D/MyLogger(3273): delta time = 0.018
09-04 15:59:39.433: D/MyLogger(3273): delta time = 0.02
09-04 15:59:39.449: D/MyLogger(3273): delta time = 0.02