Does this cause memory leak?

Recently learned about memory leaks and i’ve been remaking my combat system. Would this cause memory leak?

local Fist = {}

local CanAttack = require(game.ServerStorage.Modules.Functions.CanAttack)
local Hitbox = require(script.Hitbox)
local Debris = game:GetService("Debris")

Fist.Light = function(player)
	local char
	if player:IsA("Player") then
		char = player.Character
	else
		char = player
	end
	
	local hum = char.Humanoid
	local Root = char.HumanoidRootPart
	local Effects = char.Effects
	local combo = Effects.Combo

	if CanAttack(char) == false then
		return
	end

	local Last = false
	combo.Value += 1
	
	if combo.Value == 4 then
		Last = true
		task.delay(.5,function()
			combo.Value = 0
		end)
	end
	
	local lastHit = combo.Value
	task.delay(1,function()
		if combo.Value == lastHit then
			combo.Value = 0
		end
	end)
	
	local attacking = Instance.new("BoolValue")
	attacking.Name = "Attacking"
	attacking.Parent = Effects
	
	local s = game.ServerScriptService.Services.DamageService.Swing:Clone()
	s.Parent = char.Head
	s.Playing = true
	Debris:AddItem(s,1)
	
	if not Last then
		Debris:AddItem(attacking,.55)
	else
		local Bvel = Instance.new("BodyVelocity")
		Bvel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		Bvel.Velocity = Root.CFrame.LookVector*25
		Bvel.Parent = Root
		Debris:AddItem(Bvel,.1)
		
		task.delay(.45,function()
			local Speed = Instance.new("IntValue")
			Speed.Name = "SpeedSet"
			Speed.Value = 0
			Speed.Parent = Effects
			Debris:AddItem(Speed,0.6)
		end)
		
		Debris:AddItem(attacking,1.15)
	end
	
	hum.Animator:LoadAnimation(char.Anims.Normal["Light"..combo.Value]):Play()
	game.ReplicatedStorage.Events.TweenCam:FireClient(player,"M1")
	Hitbox.Hitbox(player,4,1,"Light")
end

Fist.Heavy = function(player)
	local char
	if player:IsA("Player") then
		char = player.Character
	else
		char = player
	end

	local hum = char.Humanoid
	local Root = char.HumanoidRootPart
	local Effects = char.Effects
	local combo = Effects.Combo

	if CanAttack(char) == false then
		return
	end

	local attacking = Instance.new("BoolValue")
	attacking.Name = "Attacking"
	attacking.Parent = Effects
	Debris:AddItem(attacking,1.3)

	local Speed = Instance.new("IntValue")
	Speed.Name = "SpeedSet"
	Speed.Value = 6
	Speed.Parent = Effects
	Debris:AddItem(Speed,0.6)

	local anim = hum:LoadAnimation(char.Anims.Normal.Light4)
	game.ReplicatedStorage.Events.TweenCam:FireClient(player,"M2")
	anim:Play()
	
	task.delay(.2,function()
		anim:AdjustSpeed(.1)
	end)

	task.wait(.5)
	anim:AdjustSpeed(1)
	
	local Bvel = Instance.new("BodyVelocity")
	Bvel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	Bvel.Velocity = Root.CFrame.LookVector*25
	Bvel.Parent = Root
	Debris:AddItem(Bvel,.1)
	
	local Speed = Instance.new("IntValue")
	Speed.Name = "SpeedSet"
	Speed.Value = 0
	Speed.Parent = Effects
	Debris:AddItem(Speed,0.6)

	Hitbox.Hitbox(player,6,1,"Heavy")
end

Fist.Block = function(player,State)
	local char
	if player:IsA("Player") then
		char = player.Character
	else
		char = player
	end
	local hum = char.Humanoid
	local Effects = char.Effects
	
	if CanAttack(char) == false then
		return
	end
	
	local anim = hum.Animator:LoadAnimation(char.Anims.Normal.Block)
	anim:Play()
	game.ReplicatedStorage.Events.TweenCam:FireClient(player,"M1")
	
	local Blocking = Instance.new("BoolValue")
	Blocking.Name = "Block"
	Blocking.Parent = Effects
	Debris:AddItem(Blocking,.5)
	
	local Attacking = Instance.new("BoolValue")
	Attacking.Name = "Attacking"
	Attacking.Parent = Effects
	Debris:AddItem(Attacking,.8)

	local Speed = Instance.new("IntValue")
	Speed.Name = "SpeedSet"
	Speed.Value = 5
	Speed:SetAttribute("State","Block")
	Speed.Parent = Effects
	Debris:AddItem(Speed,.5)
	
	task.delay(.5,function()
		anim:Stop()
	end)

end

return Fist

This should go in #help-and-feedback:code-review
Also instead of spending hours reading your code you can easily test if it causes a memory leak yourself by simulating it running thousands of times and see if the memory usage consistently increases, and if LuaHeap in the F9 console is abnormally large

1 Like

I don’t agree. Knowing what kinds of practices can cause potential memory leaks and resolving them during development is largely helpful so you can stop the problem as you write your code.

The developer console is a powerful debugging tool that can also help you spot problems at-large with your experience and can point towards what may potentially be an issue but it’s not a pinpoint, even with setmemorycategory. You have to be able to know if there’s a problem, where to look for it and what to look out for all together. It’s perfectly fine to spend a while reviewing your code.

The most common problem developers will have to watch out for when worrying about code potentially leaking memory is strong references that can’t be garbage collected such as instances in tables or connections referencing upvalues since those things need to continue to exist for the function body.

I can’t see any immediate potential memory-leak problem here (I’ll admit, I skimmed because it was a code dump with no highlights on where you’re worried about memory leaks, OP), but do try looking up some good memory management practices for Roblox and on garbage collection. The other respondent isn’t wrong to point out the developer console, but don’t play hooky on your code review either. In my experience, I use the console to debug live server problems and then do the deeper investigation with a thorough code review.

Make sure to make good use of the debug library, especially memorycategory functions, since they can help you analyse the memory usage of a large chunk of code.

5 Likes

My intention isn’t to suggest making testing and stats-reading the only way to determine memory leaks; obviously you should review your code to catch trivial mistakes. My original post is more of suggestion to an alternative method to catching leaks because, like you said, it’s just a code dump (and with all respect to the OP) I doubt anyone here would put in the effort to analyze every single line of it.

All your other suggestions and advices are perfectly valid and I also encourage the OP to utilize them.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.