Pathfinding Memory Leak

local PathfindingService = game:GetService("PathfindingService")
local ComputationQueue = {}
local ActiveRequest = 0
local Battles = workspace.Battles:GetChildren()
workspace.Battles.ChildAdded:Connect(function(Child)
	table.insert(Battles,Child)
end)
workspace.Battles.ChildRemoved:Connect(function(Child)
	local Index = table.find(Battles,Child)
	if Index then 
		table.remove(Battles,Index)
	end

end)

task.spawn(function()
	while true do
		task.wait(#Battles * 0.1)
		if #Battles == 0 then continue end
		local EarliestID
		local EarliestInfo
		local PreviousTime
		for ID,Info in pairs(ComputationQueue) do
			if not (Info.Request and Info.QueueTime) then continue end
			task.wait()
			if not PreviousTime then
				EarliestID = ID
				EarliestInfo = Info
				PreviousTime = Info.QueueTime
			elseif (tick() - PreviousTime) < (tick() - Info.QueueTime) then
				EarliestID = ID
				EarliestInfo = Info
				PreviousTime = Info.QueueTime
			end

		end
		
		if EarliestID and ComputationQueue[EarliestID] then
			local Index = ComputationQueue[EarliestID] or {}
			local Path = PathfindingService:CreatePath(Index.Params or {})
			Path:ComputeAsync(Index.StartPos,Index.GoalPos)
			print(Path.Parent)
			if Path.Status == Enum.PathStatus.Success then
				ComputationQueue[EarliestID] = {Path=Path}
			else
				local Attempts = Index.Attempts or 0
				Attempts += 1
				if Attempts > 2 then
					ComputationQueue[EarliestID].Request = nil
				else
					ComputationQueue[EarliestID].Attempts = Attempts
				end			
			end			
		end


	end
end)


local module = {}
module.ActiveRequest = ActiveRequest
module.ComputationQueue = ComputationQueue
module.RequestComputation = function(ID,StartPos,GoalPos,Params)
	if ComputationQueue[ID] then return end
	ActiveRequest += 1
	ComputationQueue[ID] = {Request=true,StartPos=StartPos,GoalPos=GoalPos,QueueTime=tick()}
	repeat task.wait() until not (ComputationQueue[ID] and ComputationQueue[ID].Request)
	ActiveRequest -= 1
	local Path = ComputationQueue[ID] and ComputationQueue[ID].Path
	ComputationQueue[ID] = nil	
	return Path
end


return module

This code has a memory leak in it and I’m unsure of where it lies. The script I call this code with is AIHandler as you can see it’s inactive with 3k rate Screenshot by Lightshot

1 Like
local Path = PathfindingService:CreatePath(Index.Params or {})
			Path:ComputeAsync(Index.StartPos,Index.GoalPos)

The large amount of processing / memory / whatever could simply be caused by this being called a large number of times

Also, change this

task.wait(#Battles * 0.1)

to

task.wait((#Battles - 1)*0.1 + 0.1)

so that it doesn’t repeatedly call the If statement a stupid number of times per second

Alright I’ll give it a shot and is there a way I get rid of the lingering memory it’s occupied even after all of the battles have stopped

Commented out the “ComputeAsync” and theres still a memory leak I’m confident it’s this module because Ive commented it out and the issue doesnt happen

Did you try the other thing I suggested (setting a minimum wait time)

task.wait(0.1)
if #Battles > 1 then
    task.wait((#Battles-1)*0.1)
end

This is a slightly varied version of what I put earlier. I noticed an issue with the previous one so use this

Yeah i tried this, the issue only happens when I pair AI’s against each other weirdly enough