Memory Repair Man

Note: This is my inaugural post on the developer forums, and I would like to ensure it is categorized appropriately. If it is not, please inform me in the comments below. Additionally, any feedback about the code is welcome. Do mind that this is the first time I made something like this
that I’ve made on my own.

Greetings, everyone. I am pleased to present my newly developed MemoryRepairMan Module, upon which I have been diligently working.

“What have you created?”

I have created a simple ModuleScript designed to facilitate the management of threads and RBXScriptConnections, allowing for their easy disconnection or cancellation. This capability aims to minimize memory leaks and enhance the overall clarity and cleanliness of your code. Makes your code cleaner overall!

“How does one use this module?”

Simple.

Example
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local MemoryRepairMan = require(ReplicatedStorage.Modules.MemoryRepairMan)

local repairMan = MemoryRepairMan.new()

local function spawnPartsLoop()
	return task.spawn(function()
		while true do
			task.wait(0.25)
			local part = Instance.new("Part")
			part.Size = Vector3.new(1,1,1)
			part.Anchored = true
			part.Position = Vector3.new(math.random(-50,50), math.random(5,25), math.random(-50,50))
			part.Parent = workspace
			task.delay(2, function()
				if part then
					part:Destroy()
				end
			end)
		end
	end)
end

local function trackPlayerConnections(player)
	local charConn
	charConn = player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local diedConn
		diedConn = humanoid.Died:Connect(function()
			if diedConn then
				repairMan:removeTask(diedConn)
			end
		end)
		repairMan:addToMemoryList(diedConn)
	end)
	repairMan:addToMemoryList(charConn)
end

local heartbeatThread = task.spawn(function()
	while true do
		RunService.Heartbeat:Wait()
	end
end)

repairMan:addToMemoryList(heartbeatThread)

local partThread = spawnPartsLoop()
repairMan:addToMemoryList(partThread)

for _,player in Players:GetPlayers() do
	trackPlayerConnections(player)
end

local playerAddedConn
playerAddedConn = Players.PlayerAdded:Connect(function(player)
	trackPlayerConnections(player)
end)

repairMan:addToMemoryList(playerAddedConn)

task.delay(15, function()
	repairMan:clearMemory()
end)

This is just a simple example I made. There aren’t any comments in it. But it should be enough to tell you how this works.

“Where can I get this?”

Memory Repair Man

Updates

Updates

Add a debug setting to see if things work as intended,
Added the ability to add tables to the memory list to clear.

That’s all, guys! Thanks for reading!

2 Likes

Quick little update, I decided to change the name of the module to _memoryCleaner. So don’t get confused when you use it in the studio and it has a completely different name.

1 Like