Enemy AI causing memory leaks?

So i’ve been having an issue with enemy ai for a while now
i’m trying to make a game where you defend a base from waves of enemies, the AI uses pathfinding
However when i play the game, usually after 1 - 10 minutes, the memory for no apparent reason spikes up and will not go down, and this results in intense lag when there are enemies on the map.
I’ve tried reducing the interval that the AI computes a path, i’ve tried destroying and dereferencing the path after an interval.
I’m theorizing that the leaks occur when a path is blocked but i don’t know for sure.


If you need full code i’ll show you

but here’s snippets of the most important bits

local function Main(Position, Part:BasePart)
	if Position then
		local Mag = (Root.Position - Position).Magnitude
		if Mag > Range.Value or Part and not CheckSight(Part) then
			Path = PathfindingService:CreatePath(table.unpack(PathParams))
			local Success = pcall(function() Path:ComputeAsync(Root.Position, Position)  end) -- the current assumption is that waypoints are causing high memory usage
			local CurrentIndex = 0

			if Success and Path and Path.Status == Enum.PathStatus.Success then
				Path.Blocked:Connect(function(BlockedIndex) -- Make this a one time connection from what i figured
					if BlockedIndex >= CurrentIndex then
						Blocked = true
					end 
				end)
				local Points:{PathWaypoint} = Path:GetWaypoints()
				if DebugMode then
					DebugPath(Points)
				end
				task.spawn(function()
					for i, v in Points do
						if not Active.Value or Humanoid:GetState() == Enum.HumanoidStateType.Dead or not Path then
							break
						end
						Waypoint = v
						CurrentIndex = i
						task.wait((16/Humanoid.WalkSpeed)/4)
					end
				end)
			else
				print("Path creation unsuccessful")
			end
		end
	end
end

local function Clean()
	Waypoint = nil
	for i, v in VisibleWaypoints do
		v:Destroy()
	end
	table.clear(VisibleWaypoints)
	if Path then if Path then Path:Destroy() end end
	Path = nil
end

task.spawn(function() -- Make Path
	repeat
		if TargetToMoveTo and not CheckSight(TargetToMoveTo) then
			--if Main() then
			Main(TargetToMoveTo.Position, TargetToMoveTo)
			local Waits = 0
			local Sight
			repeat
				Sight = CheckSight(TargetToMoveTo)
				Waits += task.wait(.2)
			until Waits >= 4 or Blocked or Sight
			if Path then
				if Path then Path:Destroy() end -- Destroy it every 2 seconds and create a new one
				Path = nil	
				Waypoint = nil
			end
			--end
		end
		task.wait(.2)
	until Humanoid:GetState() == Enum.HumanoidStateType.Dead
end)

Humanoid.Died:Connect(Clean)
Character.Destroying:Connect(Clean)

What are the actual values of memory?
Memory doesn’t usually cause lag, it causes crashes.

What is your script usage like?
I assume it’s all Server-Side?

oh lemme show in one quick second but to tell you
the memory will start at 900 mb until enemies start spawning then it goes to 1,000 - 1,200 mb
Then after a certain period of time, it spikes to 2,000 - 2,500 mb
i will show screenshots in a bit
and the AI is server side of couse