How can scripts increase CPU usage over time?

  • A game I have sometimes starts building up cpu usage after playing it for some time. It starts at 15-20 mb and in the end it gets into 25-30 with spikes of 50 mb. The fps is affected at this time.
  • I do use some runservice loops, but I think I am stopping and disconnecting most of them when not needed. And it’s loops that require constant info income, so I don’t think I can change it.
  • I would like to know what could be doing this CPU increase. If someone knows more about how garbage collection works it would help too.

I will say the best from my knowledge. Try to make scripts that deletes parts over time that don’t need to be used at that moment. I don’t think there is any way to make a script that does stuff with CPU in the system and if you can get to the CPU, you will get maybe a BSOD. (EDIT) Try putting the not used parts into serverstorage and puts it back when reloaded.

2 Likes

What are you using with runservice? It’s definitely not a good idea to be constantly using them. (Well it could be used for client uis) You need to be able to use event and signal based information collectors. It’s definitely not impossible to do that.

I use it for manipulating camera for a golf game. When a ball is striked, a renderstep function is binded. This function sets a starting time (tick()) to manipulate camera into a starting scene where it shows the players striking. 2.75 seconds after this starting tick the camera starts following the ball velocity. When it is close to the hole, it looks into the hole from the ball third person perspective. When the strike ends, the function is unbinded. All of these uses lerp for smoothiness.
Also, a serverscript has a stepped function to constantly check if a wind force will be enabled on the ball (when it is touching nothing) and change the ball’s friction depending on the terrain material it raycasts below. I tried using touched and touchended before but they seem to fail some times.

Well I can’t seem to find parts which I forgot to destroy that aren’t being used anymore on my functions, so I don’t really know what is happening. Maybe it’s the streaming enabled but I don’t really know.

The camera manipulation with renderstep wouldn’t be a big issue. However, not sure you’d need to raycast real time on the server. Furthermore you’re using physics right? The server will not be smooth thus raycasting in a stepped will be even more server heavy.

Actually show me how you are disconnecting your binds.

Yeah but I think it wouldn’t impact the player directly, only the servers. And the script activity/rate also doesn’t seem bad (about .2% activity and 30/s rate).
The server disconnects the wind/friction check like this:

function strike ball()
local conn
conn = runService.Stepped:Connect(checkwindandfriction)
ball starts flying
a repeat is added with .2 seconds wait, ending when ball stops moving, got destroyed or had its parent set to nil
conn disconnects, is set to nil and prints(“Disconnected”)
end
–This never failed, as it always printed the function was disconnected

The localscript camera works like this:
local cameraconn
function togglecamera()
pcall(
local success, message = pcall(function() runService:UnbindFromRenderStep(“CamStep”) end)
if player ingame then
togglecamera:BindToRenderStep(“CamStep”,199,cameraconn)
elseif spectating == true then
camerasubject= targettospectate
cameratype = custom
else
camerasubject= player.Character
cameratype = custom
end
end
the togglecamera happens whenever the player’s character has its parent property changed, or its ball is destroyed, or he sets mode to spectate

I realised that since it’s the client’s peripherals then it must be either the localscripts or parts as @TheKman2019 mentioned.

Yeah I’ll try to look into the code more to find some uncleared variable or part. It’s really weird because most times it doesn’t happen, but when it does it never ends until you rejoin the game.
Good thing is because the matches are set on reserved servers with timers, usually when they finish the match is when the fps is bad , so they just return to the lobby place and the fps returns to normal. But it will be a problem if I decide to make longer matches.