How would I stop memory leaks?

I need help getting rid of memory leaks because I’m making a game and need to know how to stop the frame drops too.

Kind regards, TruestBlu

1 Like

When something is no longer in use make sure to remove the active reference to it, such as with the below as an example.

local InGame = {}

    InGame[NewPlayer] = true -- The player object is used as a key in the dictionary.

    InGame[OldPlayer] = nil -- If we do not remove the player who left from the dictionary, it'll keep the player object "alive" never to garbage collect, i.e. causing a memory leak.

Okay, I’ll try and see if that works.

Hmm… Is it anything to do with loops? I have two loops. A RunService loop and a while loop.

It depends what code you are running within these loops. If you are connecting events/creating objects and not disconnecting or clearing them, these can build up and cause performance issues.

No, I’m not creating anything in these loops.

Frame drops may not necessarily be caused by memory leaks in your game. Memory leaks are usually created by some sort of recurring pattern in your code that makes connections build up that drain server/client resources. If your game has a high object count that could be the issue, the new Future lighting can be a big cause for lag as well. Basically, if you create objects that are only needed temporarily, remember to destroy them. If you have connections being created in some sort of loop, remember to disconnect old ones that aren’t needed.

If you’re using anything that runs on the 30hz pipeline a lot (wait, delay or spawn) then that may cause it to throttle, having operations take longer than usual to execute. Usually, the solution is to utilize event-driven programming (thus cutting down on the amount that’s queued on the task scheduler), or thinking smart when using them – for example, with a zombie, does it really need to recalculate every 0.0333 (1/30th) second? Zombies are dumb and clumsy, so a wait time of 2 or even 5 would be more appropriate for them. This isn’t so much a memory leak as it is causing the 30hz pipeline with the task scheduler to throttle, but I think it’s important to note nonetheless.

There are some tricky cases with connections that can cause memory leaks (see PSA: Connections can memory leak Instances!).

This is an advanced solution, but there are a couple of classes that can help mitigate these problems. Namely, the Janitor and Maid classes (see How to use a Maid class on Roblox to manage state | by James Onnen (Quenty) | Roblox Development | Medium).

Ohhhh Alright, I’ll remove the while wait loop and see if it helps with my case.

I don’t understand how to use maid, I’ve tried and there’s still luaheap leakage.

This is the only code in a loop, I’m confused.


Try binding this to heartbeat instead, since using RenderStepped will prevent the game from rendering the frame until whatever you binded to the event is complete, which can and will cause lag.


Okay, let me try this real quick.