So, I made a small module that helps keep your game from slowly turning into a laggy mess.
MemoryService basically tracks stuff you create (connections, instances, etc), and makes it easy to clean them up or reuse them. Which saves memory and keeps performance solid, especially on mobile.
What It Does
- Tracks & disconnects event connections
- Cleans up objects you don’t need anymore
- Lets you reuse things (bullet pooling, UI pooling, whatever you want)
-
- NEW: Advanced Memory Leak Detection!
- Proactively catches memory spikes and sustained memory growth.
- Provides detailed “Leak Reports” with stack traces. When a leak is detected, it tells you exactly which script and line of code created the problematic object or connection, making debugging much faster!
- Shows memory stats so you actually know what’s happening
How to Use
local MemoryService = require(game.ReplicatedStorage.MemoryService)
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
-- Tracking connections (now with creation source logged for debugging!)
local conn = RunService.Heartbeat:Connect(function()
-- Your heartbeat logic here
end)
MemoryService:TrackConnection(conn)
-- Creating and managing pooled objects (more robust pooling)
-- First, set up your pool once, perhaps in a central script:
local bulletTemplate = Instance.new("Part")
bulletTemplate.Size = Vector3.new(1,1,1)
bulletTemplate.Shape = Enum.PartType.Ball
bulletTemplate.BrickColor = BrickColor.new("Really red")
local bulletPool = MemoryService:CreateObjectPool("BulletPool", bulletTemplate, 20, Workspace)
-- Then, get an object from the pool when you need it:
local bullet = bulletPool:Get()
bullet.CFrame = CFrame.new(Vector3.new(0, 10, 0))
-- ... use your bullet ...
-- Return the object to the pool when you're done, instead of destroying it:
bulletPool:Return(bullet)
-- Cleaning up individual tracked objects (if not using pooling)
local myTemporaryPart = Instance.new("Part")
myTemporaryPart.Parent = Workspace
MemoryService:TrackObject(myTemporaryPart)
-- ... use myTemporaryPart ...
MemoryService:CleanupObject(myTemporaryPart) -- Destroys the part and untracks it
That’s it. One step and you can start optimizing.
Future Plans
- UI memory dashboard
- Better pooling presets
Download
https://create.roblox.com/store/asset/126617400324761/MemoryService