This topic definitely wasn’t the best and I’ve created a much better version which actually has some time and effort put into it, and it is this topic.
Memory usage can slow down games, disconnect players and make players leave your game. Memory usages are also really hard to identify. Be warned since this topic may be long.
Also a quick reminder, if your game is running badly on ROBLOX studio, it’ll run way better on the game itself. Roblox studio is known to use 3X more memory so if that’s your problem, you have it fixed. This topic will go through a series of ways to optimize your memory and it’ll give you a better understanding of memory usage.
To check your memory usage, press F9 and click memory:
This is not my game so you may have more to view if your a developer of a game.
MEMORY USAGE
(some lazy image I made)
Memory usage is determined by the status of your memory. For example, a high MB amount such as 32 is considered as a lot of garbage. Luckily, there is a garbage collector but some data won’t be collected. Memory usage is when a script runs too much data. When this happens, the memory won’t be able to handle the excess data and hence, it’s referred to as “garbage”. When this happens, it’ll collect up and eventually, this will flatten connections between the server and the client because of the amount of garbage getting perceived by the client.
ROOT CAUSES
Too much scripts can use too much data/memory. Scripts contain data on their source(the script and what it runs), name and class name and every property. Too much scripts will run on the memory and start producing too much data on the memory and thus, using too much memory. Too much parts also will be the root problem since each part has data for it’s properties, appearance, mesh and etc. Exploiters can also be the reason since their exploits run a LOT of data. Too much tools in starter pack will need to be replicated to the backpack and thus, using a lot of memory since your replicating data. Too much loops in your script is also a common reason since it runs data MANY times on the memory. Only use loops when you cannot do an alternative script. Now also a common mistake I see some do is parent a part before the properties are set. You may ask, Why will this use a lot of data?? Now the main reason is that the script has to check EVERY time if the part is there and thus, it uses data. Now, lets show you a demonstration of multiple codes that can heavily defect your memory usage.
while true do
wait(0.1)
local player = game.Players:GetChildren()
if player == nil then
print("No player!")
else
print(player.Name .. " is in the game!")
if player.Name == "exp_lol123" then
print("exp_lol123 is in the game!")
end
end
end
This code will use a lot of memory.
Code below will be the alternative better way with much less usage of data.—>
game.Players.PlayerAdded:Connect(function(player)
if player.Name == "exp_lol123" then
print("exp_lol123 is in the game!")
end
end)
This is a much better way and uses A LOT less memory. Here’s another example of code that uses unnesscerary amounts of memory:
while true do
wait(0.01)
if script.Value.Value ~0 then
print("Changed")
end
end
Much better practise:
script.Value.Changed:Connect(function()
print("Changed")
end)
Here’s another example, for this example what’s happening is your parenting the part to nil which you think would delete the part but in reality, the part will still use memory. Build up of this will lag your game. A part lying around like this will use memory, here’s the bad practise:
local part = workspace.Part
part.Parent = nil
Better practise:
local part = workspace.Part
part:Destroy()
Finally, disconnecting a connection. Disconnecting a connection is pretty useful since connections that are still lying around still use memory if not disconnected. This will start to cause lag problems in your games so you should never leave a connection disconnected. Here’s how:
local connection = game.Players.PlayerAdded:Connect(function(player)
print(player.Name .. " has joined!")
connection:Disconnect()
end)
(another lazy image I made)
FIXING ROOT CAUSES
Now we’ve figured out how to find root causes, how about how to fix them. Let’s start with the easiest, parts. Too many parts can lead to high memory usage but the amounts may have to be in the extreme. Unions can lower memory usage but not to the extreme. Model be great for optimizing parts(which is not this topic) so let’s move on. Let’s also now start with too many scripts. Shortening or simplifying scripts can be essential for memory usage drops. I’ve currently experienced a problem where my script was too long but with continuing the script on with another script, everything worked perfectly fine. Loops can go hard on your memory so if it’s appropriate for what you’re doing, convert them.
Why FPS is lowered because of this
FPS is probably the main key we worry about so let’s look into why FPS is lowered. Well, it’s all on communication. Communication between the server and the client transfers essential information such as scripts onto the client to render. Sometimes, garbage builds up and is mistakenly transferred which overtime, leads to more things that need to be rendered but is completely unnesscerary which lowers FPS.
Now this topic is a little long and I don’t think some of you bothered to read it but as I said above, this topic is supposed to be long since the memory is confusing. Anyways, I hope you found this useful and if there are any wrong parts, please tell me in the replies but with that out of the way, we’ve finished this topic!