Help with memory leaks

Was doing a playtest and found out that my game’s performance worsened as you played.

First question:

Does this look like a memory leak?

Tried to fix the memory leaks by setting all unneeded values to nil but it doesn’t seem to have worked.
It’s risen from around 270 to 290 in 10 or so minutes.

Second question:

How would I get rid of memory leaks?

4 Likes

Here’s a few things to do:

  • Disconnecting connections that are no longer needed, using :Once for connections only needed once
  • Destroying unneeded instances (including tweens)
  • Using streaming
  • Reducing the general instance count in your game

Doesn’t look like too much of a memory leak to me, but I’m not really one that can tell easily :sweat_smile:.

3 Likes

Additionally, playtesting in Studio causes memory to go higher, so it’s best to playtest the game through Roblox Player.

1 Like

I’m using StreamingEnabled already - no need to change anything there.

All references to instances in my main script are either inside something that gets destroyed or used enough, or are destroyed.

Most connections are used constantly, though I’ll double check.

I do have a lot of instances in my game, I will try and cut down on that.

1 Like

Don’t forget to break while true loops too. (if not needed)

Makes sense, I’ll see if this resolves anything :slight_smile:

1 Like

Quick question: If a table has references to instances, can I just set it to nil?

e.g

table = {
  thing = workspace.instance
}

table = {
 thing = nil
}

Yes. You can also use table.clear() to completely clear a table, and it will become blank.

local myTable = {"Hi", "Bye", "Good day"}
print(myTable) --> "{"Hi", "Bye", "Good day"}"
table.clear(myTable)
print(myTable) --> "{}"

I don’t think this is any better :frowning:

Use LuauHeap and other features to try and figure out what might be causing it.

From my experience, not disconnected events can cause a lot of memory leaks, make sure to double check that you disconnect events that aren’t needed. You can use modules like Maid or Janitor to help with garbage collection

All connections that aren’t needed are either destroyed (in the case of TweenService) or are used all the time. (Remotes and other things such as RunService)

its a joke sorry if its that offensive :flushed::stuck_out_tongue::stuck_out_tongue_winking_eye::nail_care:t2:

Other than what other people before me have suggested, there is not a lot you could do other than optimizing your already existing scripts to use less resources.

It’s kind of fixed… I guess? It seems to just rise and fall around 80 - 60.

Definitely better than it was. Thanks for the help!

1 Like

You usually shouldn’t use table.clear, table.clear is for when you are going to reuse that table.
table.clear removes the things inside, but doesn’t free the memory allocated for that table, so if that is never used, it causes what is essentially a memory leak.

When it comes to instances, your best bet is to always destroy what is not used. The main issue with Instances is that they don’t deal with self-references (I forgot the word, let’s go with that) properly.

This is a popular example:

local BindableEvent = Instance.new("BindableEvent")

BindableEvent.Event:Connect(function()
    (Bindable.Event) --> this causes a memory leak forever, IIRC
end)

A lot of things, like Tweens, are weirdly instances, so your best bet is to deal with them automatically, always properly destroy them and remove their references. I used to have QuickTween which is just a wrapper for creating Tweens that automatically deals with destroying things after a tween is finished as I wouldn’t use it anymore anyways in any occasion.

If you’re not already, but I think everyone is by now, use a Lua implementation of Signal, don’t use BindableEvents.

1 Like

Yeah, you’re right about table.clear. I kind of assumed it was within another table, and for some reason (force of habit) I always clear a table before I then assign the variable that holds it to nil. Thanks for the clarification.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.