MemoryService — Easy Memory Optimization for Roblox

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

4 Likes

I would use this in the future once you improved it abit more I think you should make a feature where once you join the game in studio test and you have the plugin open every single time a memory leak or something else that’s bad for memory happens it alerts you and tells you which script is causing it if you make something like that for like a free plugin I will 100% use this!

Its not really a plugin, its a module. Nor do I have plans to turn it into a plugin, but I will take your suggestion into consideration!

1 Like

Just added your suggestion, feel free to try it out

Hey, why collectgarbage(“count”) and not gcinfo()?