Could this cause a memory leak?

My game has been crashing after around 30 minutes of gameplay. This is one of the only active scripts that might cause it?

Also how much client memory usage is too much? It just slowly ticks up for me.

In Dev Menu

Here is the script:

task.wait(1)
local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer

local char = Player.Character
local origRightS = char:WaitForChild("Torso"):WaitForChild("Right Shoulder").C0
local origLeftS = char:WaitForChild("Torso"):WaitForChild("Left Shoulder").C0


local m = Player:GetMouse()

local UIS = game:GetService("UserInputService")

local IsEquipped = false

game:GetService("RunService").RenderStepped:Connect(function()
	if IsEquipped == true then

		if char.Torso:FindFirstChild("Right Shoulder") then
			char.Torso["Right Shoulder"].C0 = char.Torso["Right Shoulder"].C0:Lerp(CFrame.new(1, .65, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), 1.55, 0, 0) , 0.1)
		end
		if char.Torso:FindFirstChild("Left Shoulder") then
			char.Torso["Left Shoulder"].C0 = char.Torso["Left Shoulder"].C0:Lerp(CFrame.new(-1, .65, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), -1.55, 0, 0) , 0.1)
		
		end

	else

		if char.Torso:FindFirstChild("Right Shoulder") then
			char.Torso["Right Shoulder"].C0 = char.Torso["Right Shoulder"].C0:lerp(origRightS, 0.1)
		end

		if char.Torso:FindFirstChild("Left Shoulder") then
			char.Torso["Left Shoulder"].C0 = char.Torso["Left Shoulder"].C0:lerp(origLeftS, 0.1)
		end



		end

end)


char.ChildAdded:Connect(function()
	for i,v in pairs(char:GetChildren()) do
		if v:IsA("Tool") then
			if v:FindFirstChild("HoldArmsStill") then

			else
				IsEquipped = true
			end
			end
			end
end)

		char.ChildRemoved:Connect(function(instance)
			if instance:IsA("Tool") then
				IsEquipped = false
				end
end)

game.ReplicatedStorage.Look.OnClientEvent:Connect(function(PlrAgain, neckCFrame, RsCFrame, LsCFrame)
	local Neck = PlrAgain.Character.Torso:FindFirstChild("Neck", true)
	local Rs = PlrAgain.Character.Torso:FindFirstChild("Right Shoulder", true)
	local Ls = PlrAgain.Character.Torso:FindFirstChild("Left Shoulder", true)

	if Neck then
		Neck.C0 = neckCFrame
	end

	if Rs then
		Rs.C0 = RsCFrame
	end

	if Ls then
		Ls.C0 = LsCFrame
	end
end)

while task.wait(0.05) do 
	game.ReplicatedStorage.Look:FireServer(char.Torso["Neck"].C0, char.Torso["Right Shoulder"].C0, char.Torso["Left Shoulder"].C0)
end


Under normal circumstances, the code snippet above would cause memory leakage. The reasoning being that the connected function contains a reference back to the hosting character Instance, which results in a cycle of references that will keep the character in memory for as long as the lingering connection is not disconnected. This is problematic for permanent scripts, as :Destroy() is never actually called on characters when they despawn, so such a circumstance would have to be resolved by manually calling :Disconnect(). However, since this script is only temporary (it presumably dies with the character anyway) that would never be an issue–nor would it ever appear as a ‘constant uptick in memory’.

Other than that, from the code you’ve posted, I don’t see anything that jumps out at me as a potential memory leak. Is there anything else that you think might be the culprit?

1 Like

Im a bit confused? Mind showing me how its done? Im really bad with lua garbage collection and stuff like that.

This basically means every time a player dies, it does not clean the memory so it will eat memory over time

A really dumb fix I use for this is just destroying the character when their character is removed (put this in a script in serverscriptservice if you want to try it)

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterRemoving:Connect(function(character)
        character:Destroy()
    end)
end)

The difference between being removed and being destroyed is that if something is removed, all connections to it stay the same.
If something is destroyed, any connections it may have had are deleted and it saves memory

EDIT: you also have to enable this feature

also I think the same thing happens with players when they leave

3 Likes

Correct. Much to the dismay of just about everyone, Roblox does not call :Destroy() on Players either. Also, good call on bringing up that announcement.

It’s worth reclarifying, that I only brought any of this up to point out the danger of the habit itself. Since OP’s script is only temporary, it presumably wouldn’t be an issue under this specific circumstance. If OP is experiencing consistent memory leakage over time, then the issue lies elsewhere.