在我尝试优化我的Pathfinding时,我遇到了一些非常严重的内存泄漏。以下是一些不断发布的消息。
07-03 15:26:14.179 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4104K, 29% free 14506K/20195K, paused 2ms+3ms
07-03 15:26:14.689 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4062K, 29% free 14506K/20195K, paused 2ms+3ms
07-03 15:26:15.199 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4032K, 29% free 14537K/20195K, paused 1ms+3ms
07-03 15:26:15.709 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4103K, 29% free 14506K/20195K, paused 2ms+3ms
07-03 15:26:16.209 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4062K, 29% free 14506K/20195K, paused 2ms+3ms
07-03 15:26:16.729 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4020K, 28% free 14549K/20195K, paused 2ms+2ms
07-03 15:26:17.229 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4118K, 29% free 14506K/20195K, paused 2ms+3ms
07-03 15:26:17.739 23501-23503/com.totalannihilationroadrage D/dalvikvm﹕ GC_CONCURRENT freed 4062K, 29% free 14506K/20195K, paused 1ms+4ms
由于以前没有泄漏,我只能假设它是我进行更改的唯一功能,它将在此处:
public void addChild(int row, int col, TiledMap tiles, Node currentNode, Node goal, Direction facing)
{
Node child;
Node tempChildNode = new Node(row, col, currentNode.gCost, currentNode.fCost, currentNode, facing);
if((row >= 0 && col >= 0) && (row <= tiles.height && col <= tiles.width))
{
if (tiles.isPassable(row, col))
{
if (!isClosed(tempChildNode, closedList))
{
double g = currentNode.gCost + getDistanceFromParent(tempChildNode, currentNode);
double f = g + getDistance(row, col, currentNode);
if(isOpen(tempChildNode, openList))
{
child = new Node(row, col, currentNode.gCost, currentNode.fCost, currentNode, facing);
if(child.gCost > g)
{
child.fCost = f;
child.gCost = g;
child.parentNode = currentNode;
}
}
if(!isOpen(tempChildNode, openList))
{
child = new Node(row, col, g, f, currentNode, facing);
openList.add(child);
if(child.gCost > g)
{
child.fCost = f;
child.gCost = g;
child.parentNode = currentNode;
}
}
}
}
}
}
修改的部分是:
if(isOpen(tempChildNode, openList))
{
child = new Node(row, col, currentNode.gCost, currentNode.fCost, currentNode, facing);
if(child.gCost > g)
{
child.fCost = f;
child.gCost = g;
child.parentNode = currentNode;
}
}
if(!isOpen(tempChildNode, openList))
{
child = new Node(row, col, g, f, currentNode, facing);
openList.add(child);
if(child.gCost > g)
{
child.fCost = f;
child.gCost = g;
child.parentNode = currentNode;
}
}
改变自:
Node child = getChildFromOpen(row, col);
if (child == null)
{
child = new Node(row, col, g, f, currentNode, facing);
openList.add(child);
}
else if(child.gCost > g)
{
child.fCost = f;
child.gCost = g;
child.parentNode = currentNode;
}
getChildFromOpen()
是一个循环遍历openList
的函数,如果找到则返回Node
,如果不是null
则返回isOpen
。此功能已替换为boolean
,如果找不到true
false
,则{{1}}会返回{{1}}。