Memory leak when player respawns

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?

My client memory usage is usually around 1350 to 1500 mb.
image

Also, after a bit of digging in LuaHeap, I found that each reset, the “thread” object increases in size and count. I don’t know exactly what this means though. What I’m thinking is that somewhere in my scripts a thread isn’t being closed. Is there a way to find out where this is happening?

Anything below 2000mb shouldn’t concern you, that’s actually quite good. Only raise concerns of memory if it goes over 2000mb

I understand that, but the problem is that I’m worried that memory will continue to pile up to the point where it goes over 2000 mb, making the game laggy and unplayable.

I also noticed the object “enumitem” gradually increasing. I don’t really know why enum items are increasing. Are they variables in a script or something?

My memory is about 1600-1700 when I am playing with 3-4 players, but I see other games with 20-30 players with 1200-1300 mb. I think I’m doing something wrong with my code.

The problem is that my luaheap memory stays constant while my total memory (graph) increases and doesn’t come down. This makes it almost impossible (at least that’s what I know of) to find out what is causing the leak.

Using events rather than loops is preferable, when possible. It would reduce the amount of Tweens being created, although to be honest, if health is changing very frequently, then there wouldn’t be that much of a difference between using loops

I don’t think that the tweens are responsible for the memory leak, now that I have a better understanding on how you’re using them

Another thing you should check is to make sure that you aren’t creating a connection which isn’t disconnected inside of another connection, that would definitely result in a memory leak


@himynameisKyliee2007 By the way, is the memory leak occuring when testing on the Player? If it isn’t, then it’s relatively safe to ignore, although do check on the Player again from time to time, and take note of the script so that you’ll remember where to look for the potential cause