Memory leak - high usage in physics/broadphase

Hi, recently my game has suddenly been having users crash from high memory usage. The game already typically has high memory but players almost never crashed, and now it’s very frequent.
The main increase in data usage is in physics/broadphase, at over 600mb.
This typically doesn’t use up much memory but it suddenly does.

1 Like

What script do you think it’s coming from? Check all scripts with the RunService, or while do loops.

I have no idea, no scripts have been updated since the issue started afaik. Also, why loops/run service if it’s to do with a physics property?

If you dont wait enough, or you do stuff to fast, it’s a very easy way to get a huge memory leak.

One of the main reasons of the Memory Leak(s) might be a script running code several times really fast. If you have a loop that creates parts/changes something from them (or just a for loop) I recommend you using RunService.Heartbeat:Wait() for yielding. You can put it at the end of the loop:

for X = 1, 1000 do
    -- Code Here

You can also use the Maid Class that is really useful to manage connections/states and avoid memory leaks.

This isn’t a good idea if what’s being done needs to be done in a timely manner. At max (without using a fps unlocker, and even then it’s only on the client) Roblox runs at 60 fps. This is roughly ~0.017 seconds in-between each frame. This means that if you were to wait for the next frame each step, the loop would be completed in 17 seconds. For loops are actually optimized so that you don’t have to use a wait, although given high enough values (10 million or so) they will start to cause lag.

Also, a little side note, you can use task.wait() instead of RunService.Heartbeat:Wait() now since task.wait uses the heartbeat event. Less typing.

You can check which script is using the most processing power using the script performance tab. You’ll find this in the view tab if I recall correctly.

ok so apparently one of my devs was working on something (pointless) and left it past the floating point in workspace, 2 of the same block with the same 2 scripts in each that obliterated the physics engine by using runservice with no proper wait, making the memory in physics go from around 1-2 to 600 for one person, 1200 for another, and 5000 for me
Thanks for the tips though, will consider this stuff if I ever have similar issues again
lesson? don’t trust idiots (this is like the 4th stupid mistake he’s done)