Android游戏GC滞后?怎么解决?

时间:2014-03-03 02:30:41

标签: java android multithreading garbage-collection

我创建了a small game,当我播放它时,我在一定时间间隔内遇到非常大的滞后尖峰;当我查看logcat输出时,我可以看到滞后是由垃圾收集器引起的。现在我已经调整了我的代码以遵守关于如何制定for循环以不创建垃圾的所有规则,并且我在游戏开始之前创建了所有对象。然而,滞后仍然存在。

我看看像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;
            }
        }
    }

1 个答案:

答案 0 :(得分:-1)

你应该对你的程序进行堆转储并使用MAT或JHAT进行分析,看看当你有大量内存使用时你的内存是什么,