【发布时间】: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