0

下面是我的游戏循环和 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
4

2 回答 2

0

我能够减少我的应用程序显示的相当多的挑剔效果,我认为这主要是由于不同的 deltaTimes。我现在的解决方案是在我的主循环中计算每 5 次迭代的平均增量时间,并将其传递给每个更新/绘制调用。然后我继续每 5 次迭代执行此操作,但我也与上一次迭代进行比较,有效地将每个 deltaTime 计算之间的任何差异减少一半。我将继续研究优化这一点的方法。请让我知道这种方法是否不错!

   @Override
public void run() {

    Canvas canvas = null;
    while (isRunning) {
        if (!gamePanel.paused) {


            if (lastAverage != deltaTime) {
                deltaTime = (deltaTime + lastAverage) / 2;
            }

            startDraw = System.currentTimeMillis();
            canvas = surfaceHolder.lockCanvas();
            // synchronized (surfaceHolder) {
            gamePanel.update(deltaTime);
            gamePanel.draw(canvas);
            // }
            //

            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
                endDraw = System.currentTimeMillis();
            }
            //

            currentAverage += (endDraw - startDraw) / constant;
            tickTracker++;

            updateAverage();

        }
    }
}

private void updateAverage() {

    if (tickTracker == 5) {
        lastAverage = this.deltaTime;
        currentAverage = currentAverage / tickMax;
        this.deltaTime = currentAverage;
        currentAverage = 0;
        tickTracker = 0;
    }

}
于 2013-09-06T20:10:03.973 回答
0

修岛凤凰是对的——虽然不会很多,但在循环中,事情会堆积起来。Draw 被称为快速和频繁,你的 looper 也是如此,所以当你这样做时:

  MyLogger.getInstance().log(
                             "delta time = " + (endDraw - startDraw) / 1000.f);

本质上做的是:

添加 2 个浮点数并除以结果,获取该结果并将其转换为字符串。然后(也许复制?)该字符串并将其附加到字符串“delta time =”,然后(也许?)再次复制组合字符串作为传递给 .log() <- 您已经获得了一个新实例的参数每一次。在快速循环中会产生大量开销......

于 2013-09-04T23:18:27.760 回答