Connection and Memory Leak

Advanced topic, decent scripting knowledge and experience required.

by LadyCelastia

Author Avatar


Have you ever accidently pressed F9 and then a big menu with a lot of information pops up? That's called the Developer Console. If you go to the Memory tab and scroll all the way to the bottom, you can see "UntrackedMemory". If you're viewing the Client Memory, it'll always be 0. But if you view the Server's Memory, you can see it fluctuating.

You may be asking: "Why should we care about all of these information?". Well, here's the catch. If a server uses too much memory, it'll start lagging. And if the memory usage is so great, it'll crash.

You may also be asking: "Why should we care about UntrackedMemory the most?". Well, UntrackedMemory is the most difficult to handle and A LOT of game developers suffer from it. I have seen many famous game developers fail to solve the UntrackedMemory problem and have their game extremely laggy. I'm here to tell you how UntrackedMemory generates and how to prevent it.

How is it produced?

It's actually really simple to generate UntrackedMemory. It can be but not limited to:

workspace.Part.Parent = nil -- Setting an instance's Parent to nil. (Not destroying)

workspace.MeshPart:Destroy() -- Destroying a Mesh/MeshPart

game.Players.PlayerAdded:Connect(function(Player) -- Connections (The biggest issue)
    print("A player has joined!")

local New_Part ="Part", workspace)
print(New_Part.Name) -- Referencing a destroyed object

Side note: Destroying a MeshPart might no longer generate UntrackedMemory because roblox is constantly updating.

In this case, we'll focus on Connections. (Because this is where people have the most issue with)

You should know what a Connection is since this is an advanced topic.


Connections are useful, they are used in almost every script. Even a simple touch detector has a connection in it.

This is a simple touch detector:

    print("My part is touched!")

You may think there's nothing wrong with it. Then you're completely wrong.

Connections take up quite a lot of memory space. And if you make a lot of them, things will go wrong.

Take this as an example:

while wait(1) do
    local New_Part ="Part", workspace)
    New_Part.Position =, 5, 0)
    New_Part.Anchored = true
        print("I have been touched!")

This simple script will generate 1 connection every second. If you wait long enough, the server may start lagging. It's not because of the amount of instances, it's because of the high UntrackedMemory. By that point, if you press F9 and check the server's Memory, it'll shock you.

The UntrackedMemory can go up to several thousands of MB, which is more than enough to crash a server. That's insane.

Preventing Memory Leak

So how do we prevent UntrackedMemory? It's actually quite simple.

local New_Part ="Part", workspace)
local cn; cn = New_Part.Touched:Connect(function(Object)
    print("I have been touched!")
    cn:Disconnect() -- Disconnecting a connection when it's not needed anymore.

local New_Part2 ="Part", workspace)
    print("I have been touched!")
    New_Part2:Destroy() -- Destroying the part that's connected will automatically disconnect the connection.

while wait(1) do
    local New_MeshPart ="MeshPart", workspace)
        -- Avoid putting connections into a loop.

local New_Part3 ="Part", workspace)
    local New_Part4 -"Part", workspace)
        -- Avoid connection inside a connection!

    -- Avoid creating seperate threads!
    while wait(1) do
        print("I am a string!")
        -- Especially with while loops in them!

    -- Avoid creating seperate threads!

    -- Avoid creating seperate threads!

-- Always put local before variables otherwise they may not be picked up by Lua Garbage Collector!
local This_Is_A_Variable = true
Do_Not_Be_This_Guy = false -- Do not be this guy!

Additional methods:


This concludes the topic, I hope it helped. Now be more aware of Memory Leak!

View in-game to comment, award, and more!