Memory leak when player respawns

I have a memory leak in my game where if the player resets or gets killed. I’ve checked every local script and tried disconnecting all events, but memory still stays up when I respawn.

Right now, at the end of every script, I have this piece of code:

humanoid.Died:Once(function()
	-- disconnect all events and set them all to nil
end)

Shouldn’t all the memory related to the character be cleared after the character is destroyed, or is there something I’m missing?

Any help would be appreciated!

1 Like

How are you disconnecting the events? As you may not be doing it correctly, in this case Roblox is probably creating more and more events.

1 Like

This is how I disconnect the events:

character:WaitForChild("Humanoid").Died:Once(function()
	if RSConnection then
		RSConnection:Disconnect()
		RSConnection = nil
	end
end)
1 Like

You may be initializing a new connection if you have a charactedadded event somewhere in your code in perhaps a gui. you may have turned on the setting “preserveonrespawn” on a ui, which will trigger the characteradded event.
eg:

plr.CharacterAdded:Connect(function()
      plr.Character.HumanoidRootPart.Touched:Connect(function()
             print("this isn't cleared up if the parent ui is set to preserveondeath") -- will keep getting initialized every time on death
      end)
end) 
1 Like

Make sure that Workspace’s PlayerCharacterDestroyBehavior property is set to either Enabled or Default (Default should be equivalent to enabled)

Also, consider using (or making) a janitor module to help keep track of connections :slight_smile::+1:


@himynameisKyliee2007

If a strong reference is held for a character that was destroyed, it will still continue to exist in memory. As an example, if you’re storing the characters inside of a table that’s used in a function connected to an event, unless you make sure that you remove the character from the table once it’s destroyed, or you disconnect the function from the event, the character will continue to exist in memory

1 Like

Hi, thanks for getting back to me.

To be clear, I have a client memory leak, so the character destroy behavior doesn’t really do much because according the the link it affects server memory. I could be wrong though.

I have a lot of connections that I don’t specifically disconnect but are connected to the character or instances that are children of the character. I’d assume they are disconnected when the character is respawned, but I’m not sure.

Another thing is I have a lot of tweens being created in run service loops. Does this cause memory leaks?

Another thing is that I have a camera shaker Oop object that is created but never “destroyed” when the character dies. Do I have to worry about this?

1 Like

Hi there,

Are you talking about the reset on spawn property in screenguis?

1 Like

Even though Tweens aren’t created using Instance.new, they’re still an Instance. I’ve never encountered a situation where I needed to create a Tween from inside of a RunService loop, and I wouldn’t be surprised that it’s either the cause of the problem, or related to it

Out of curiosity, are you creating a new Instance, tweening it, then destroying it each frame? If so, that would definitely tank the performance of your game. I recommend finding ways to reuse instances instead

1 Like

This is what doing:

TweenService:Create(...):Play()

If this is a bad practice, I have to tween things in a camera module for smooth movement. What do I do instead?

1 Like

For janitor modules, do you recommend the Janitor module or goodsignal? I’m just curious on which one works the best

1 Like

My tutorial has some info on this, the posts under it provide extra info too so check those out aswell:

1 Like

By the way can you show the memory usage when the leak happens? Can’t you also see what’s using up the most memory in the log to identify what the issue is? I doubt its resetting because the destroy behaviour is automatically on by default.

1 Like

I looked at the memory graph and it seems like luaheap seems stay the same (100 mb) but my client memory usage still went up by 30 mb. Is there something else I’m missing?

1 Like

My client memory usage has jumped 20 mb but my placememory has is still around the same

1 Like

I don’t know if I’m doing something wrong, but when I paste the code given from the tutorial you sent in ServerScriptService and reset character, my camera stays in the same place my character disappears, and I can’t move.


and I’m getting these errors:

I discovered that I didn’t destroy the raycasthitboxv4 object that I created, I just added it to janitor and it’s doing well (luaheap memory doesn’t increase every respawn)

However, the problem is that when I fight other players, the memory goes up and stays up after I respawn (the script i mentioned before goes up in memory).

I also noticed a bit of lag when I move around and dash, but my fps (when I press shift + f5) is still at a steady 60. Does anyone know why this is happening?

Essentially, it would be best to find a way to create the Tween outside of an infinite loop

I use my own janitor module, but both Janitor and GoodSignal have a good reputation. The Janitor module allows you to clean up instances as well, though

If the functions that are connected to events are anonymous, it makes it harder to identify where the memory leak is located. You’ll need to use the debug library’s profilebegin and profileend functions

Oh yeah that code that I gave wasn’t 100% complete I was just giving an idea of how to do the stuff I guess but at the time I didn’t realise the new characterdestroy behaviour feature was added.

Can you show the full client memory usage? Instead of just parts of it? That’s what I’m looking for, anything 2000mb or below shouldn’t concern you.

The problem with creating a tween outside a runservice loop is that the values that change the object tweened varies. For example, a health bar udim2 size would vary based on health.

Should I use .changed instead?