Memory leak fixing

Hi, I located a memory leak in one of my games - very severe one! It was leaking 4GB+ of memory. But I am not sure how to fix it. If I comment out v:Update(), the memory leak disappears, but that’s not really a solution. Could someone give me some tips? I am not particularly well-versed in this.

function Tycoon:Init()
	self.Owner:SetAttribute("LoadingType", "SettingUpTycoon")
	self.Model = NewModel(template, self._spawn.CFrame)
	self.Model:SetAttribute('PlayerOwnerName', self.Owner.Name)
	self.Model.DropStorage:SetAttribute('PlayerOwnerName', self.Owner.Name)
	self._spawn:SetAttribute('PlayerOwnerSpawn', self.Owner.Name)
	self._spawn:SetAttribute("Occupied", true)
	self._spawn.ConnectedPlate.Value.Parent = game.ServerStorage
	self.Model.PlayerUsernamePartHolder.SurfaceGui.TextLabel.Text = self.Owner.Name
	
	self.Components = {}
	
	self.Owner.RespawnLocation = self.Model.Spawn

	self:LockAll()
	self:LoadUnlocks()
	self:WaitForRebirth()
	self:WaitForExit()
	self.Model.Parent = workspace
	self.Owner:LoadCharacter()
	self.Owner:SetAttribute("LoadingType", "SettingUpModules")
	
	if self.RunConnection then
		self.RunConnection:Disconnect()
	end
	
	self.RunConnection = game:GetService("RunService").Heartbeat:Connect(function(dt)
		if self.Components[componentFolder.Dropper] then
			for _, v in pairs(self.Components[componentFolder.Dropper]) do
				v:Update()
			end
		end
	end)
	self.Owner:SetAttribute("LoadingType", "Finished")
end

Can you show the code for v:Update()? Its possible that the function doesnt clean everything up and since it runs every frame it can cause a big leak

1 Like

Yes, it belongs to a dropper. Here is the dropper code:

local dropsFolder = game:GetService("ServerStorage").Drops
local MainRemote = game:GetService("ReplicatedStorage").Remotes.MainRemote
local Debris = game:GetService("Debris")

local Dropper = {}
Dropper.__index = Dropper

function Dropper.new(tycoon, instance)
	local self = setmetatable({}, Dropper)
	self.Tycoon = tycoon
	self.Instance = instance
	self.Rate = tonumber(instance:GetAttribute("Rate"))
	self.DropTemplate = dropsFolder[instance:GetAttribute("Drop")]
	self.Active = true
	self.DropSpawn = instance.Spout.Spawn
	self.lastDrop = os.clock() - self.Rate
	
	local Proximity = Instance.new("ProximityPrompt", instance.Spout)
	Proximity.ActionText = "Disable"
	Proximity.Triggered:Connect(function(player)
		if player.Name == self.Tycoon.Model:GetAttribute("PlayerOwnerName") then
			self.Active = not self.Active
			if self.Active == true then
				Proximity.ActionText = "Disable"
			else
				Proximity.ActionText = "Enable"
			end
		end
	end)

	return self
end

function Dropper:Drop()
	if self.Active == true then
		local drop = self.DropTemplate:Clone()
		drop.Position = self.DropSpawn.WorldPosition
		drop:SetAttribute('PlayerName', self.Tycoon.Owner.Name)

		drop.Parent = self.Tycoon.Model.DropStorage
		if self.Tycoon.Owner:GetAttribute("ClientSidedBalls") == true then
			drop:SetNetworkOwner(self.Tycoon.Owner)
			
		end
		
		if drop:GetAttribute("RenderType") and game.ReplicatedStorage.MarbleRenders:FindFirstChild(drop:GetAttribute("RenderType")) then
			MainRemote:FireAllClients("MarbleRender", drop)
		end
		
		if workspace:GetAttribute("SlowedDespawn") == false then
			Debris:AddItem(drop, 30)
		else
			Debris:AddItem(drop, 45)
		end
	end
end

function Dropper:Update()
	local currTime = os.clock()
	if (currTime - self.lastDrop) >= self.Rate then
		self.lastDrop = currTime
		self:Drop()
	end
end

return Dropper
1 Like