まず、これは私が取り組んでいる Android ゲームの一部です。ガベージ コレクターは約 3 秒ごとに実行されるため、ゲームに短い (ただし顕著な) 遅延が発生します。コード内の 1 つのメソッドに絞り込みました (以下に貼り付けます)。この部分を使用しない場合、代わりにガベージ コレクターが約 11 秒ごとに実行され、ラグが少なくなります。このコードは、ツリー構造を使用してオブジェクトとの衝突を検出するオブジェクトの一部です。オブジェクト topLeft、topRight、bottomLeft、および bottomRight は、衝突について再帰的にチェックされる同じタイプのオブジェクトです。私は主に、これを各フレームで実行した場合に大量のゴミが蓄積するものがここにあるかどうかを知りたいと思っていました.
public HashSet<Integer> getCollisionTiles(Rect r)
{
resetTempList();
if(topLeft!=null && topLeft.containsTiles() && Rect.intersects(r, topLeft.getBounding()))
topLeft.addCollisionTiles(tempList, r);
if(topRight != null && topRight.containsTiles() && Rect.intersects(r, topRight.getBounding()))
topRight.addCollisionTiles(tempList, r);
if(bottomLeft != null && bottomLeft.containsTiles() && Rect.intersects(r, bottomLeft.getBounding()))
bottomLeft.addCollisionTiles(tempList, r);
if(bottomRight != null && bottomRight.containsTiles() && Rect.intersects(r, bottomRight.getBounding()))
bottomRight.addCollisionTiles(tempList, r);
return tempList;
}
private void addCollisionTiles(HashSet<Integer> tList, Rect r)
{
if(level==maxLevel)
{
for(Integer i: keyListTiles)
tList.add(i);
}
else
{
if(topLeft!=null && topLeft.containsTiles() && Rect.intersects(r, topLeft.getBounding()))
topLeft.addCollisionTiles(tList, r);
if(topRight != null && topRight.containsTiles() && Rect.intersects(r, topRight.getBounding()))
topRight.addCollisionTiles(tList, r);
if(bottomLeft != null && bottomLeft.containsTiles() && Rect.intersects(r, bottomLeft.getBounding()))
bottomLeft.addCollisionTiles(tList, r);
if(bottomRight != null && bottomRight.containsTiles() && Rect.intersects(r, bottomRight.getBounding()))
bottomRight.addCollisionTiles(tList, r);
}
}