【问题标题】:Android Game GC Lag? How to fix It?Android 游戏 GC 滞后?如何修复它?
【发布时间】:2014-03-03 02:30:41
【问题描述】:

我创建了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;
            }
        }
    }

【问题讨论】:

  • 没有任何代码很难分辨。 GC 清理太多意味着您分配了太多内存...检查您的来源是否有“新”语句,看看您是否可以重用一些对象,...
  • 还要看看你正在加载的图片的大小...
  • 图像非常小:56 x 72 等。而且我只有在应用程序开始时有新语句,而不是在 .
  • 大胆猜测,也许您自己进行了物理/碰撞并在那里创建了大量向量/矩阵?
  • 我创建了 int x 和 int y 来检查碰撞。那是错的吗?我添加了碰撞代码。

标签: java android multithreading garbage-collection


【解决方案1】:

您应该对您的程序进行堆转储并使用 MAT 或 JHAT 对其进行分析,并查看当内存使用量很大时您的内存填充了什么,

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多