安卓游戏GC滞后?如何修复它



我创建了一个小游戏,当我玩它时,我在某些时间间隔内遇到了非常大的延迟峰值;当我查看 logcat 输出时,我可以看到滞后是由垃圾回收器引起的。现在我已经调整了我的代码,以遵守有关如何制定循环以免产生垃圾的所有规则,并且我已经在游戏开始之前创建了所有对象。但是,滞后仍然存在。

我看了像Flappy Bird这样的游戏,尽管它很简单,但它没有任何延迟。我做错了什么?

如果需要,我会发布代码。

这是碰撞代码:

branchesSize = Level.branches.size();
    for (int x = 0; x < branchesSize; ++x) {
        if (yPos <= Level.branches.get(x).yPos
                + Level.branches.get(x).height
                && yPos >= Level.branches.get(x).yPos) {
            if (xPos <= Level.branches.get(x).xPos
                    + Level.branches.get(x).width
                    && xPos >= Level.branches.get(x).xPos) {
                // DEATH
                death();
            }
        }
        if (yPos + height / 2 >= Level.branches.get(x).yPos
                && yPos + height / 2 <= Level.branches.get(x).yPos
                        + Level.branches.get(x).height) {
            if (xPos + width <= Level.branches.get(x).xPos
                    + Level.branches.get(x).width
                    && xPos + width >= Level.branches.get(x).xPos) {
                // DEATH
                death();
            }
        }
        if (yPos >= Level.branches.get(x).yPos
                && yPos <= Level.branches.get(x).yPos
                        + Level.branches.get(x).height) {
            if (xPos + width <= Level.branches.get(x).xPos
                    + Level.branches.get(x).width
                    && xPos + width >= Level.branches.get(x).xPos) {
                // DEATH
                death();
            }
        }
        // Collect Nuts
        nutsSize = Level.nuts.size();
        for (int y = 0; y < nutsSize; ++y) {
            if (yPos <= Level.nuts.get(y).yPos) {
                nuts += 1;
                Level.nuts_available.add(Level.nuts.get(0));
                Level.nuts.remove(0);
                nutsSize -= 1;
            }
        }
    }

你应该对你的程序进行一堆转储,并使用MAT或JHAT分析它,看看当内存使用量很大时,你的内存被填满了什么,

最新更新