I have a habit of avoiding defining variables at the top of my scripts because of a concern that if there are a lot of variables defined this way spread across a lot of scripts, there might eventually be an issue of memory usage.
For example, with this code:
local service1, service2 = …
local guiElem = …
function SettingsGui.Setup(MiscPlayerData)
-- Uses variables defined above
end
I would have the tendency to rewrite it as follows:
function SettingsGui.Setup(MiscPlayerData)
local service1, service2 = …
local guiElem = …
-- Uses variables defined above
end
My thought is that if I were to have hundreds or thousands of scripts within a game, moving all of the variables from being defined at the top of the scripts to within a function might make a difference in memory use. After all, when defined inside a function, once the function finishes execution the variable(s) used would be gone — as opposed to if they were defined at the top of the script, where the variables would simply linger on forever.
I don’t actually know much about the exact details of how Roblox internally handles memory/variable definitions/etc., so I’ve never been sure about this. But this programming habit is something I’ve had for quite a while, and I was finally thinking I should probably scrap it. It probably falls entirely within the category of premature optimization, now that I really think on it?
What’re y’all’s thoughts on this? Yea or nay? Completely inconsequential? I’d like to know.
first of all itsnot good to have thousands of scripts in a game its good to have few/some scripts and modules that have the functions needed
if you want a variable to get garbage collected after you done using it simply make it nil
local num = 3
doalotofstuff(num)
num = nil -- setting it to nil will make the garbage collector free it
or you can use do … end so your variable will end at the end of the scope
do
num = 2
print(num)
-- num will get garbage collected herehere
end
and i donot think its good to use functions for that purpose its much faster to use variable the normal way
also variables shouldnot be that much of a concern devices got alot of gbs of ram and a variable can take 8bytes?16bytes?32bytes? that isnot alot of memory to worry about
lets say u got 1000 number variables each one is 8 bytes that is 8 * 1000 = 8kb not that much
what you should take into consideration more is the performance and to not send alot of data from server to client / client to server and to not create variables when they arnot needed
Zero concern. Like, nonexistent. The amount of memory that a few dozen variables take up is minuscule to the amount of memory required to run a game. It’s like a drop in the ocean. A single CFrame uses around 20 bytes. A single server could potentially use multiple gigabytes of memory and still run fine. One gigabyte is 1,000,000,000 bytes — that’s a lot of CFrames. Roblox games wouldn’t be possible if a few local variables at the top of a script would slow anything down.
My current project has around 100 module scripts and it’s pretty normal for a module to have 10-40 lines of code at the top that are just local variables. I don’t define all of them at once, but the variables tend to build up as a module grows whenever I need to require a new module or reference a service/instance.
I have never run into an issue with memory in this way. I have always created my variables—
At the top of the code, if it is a variable which controls some element of the code (like, for example, a true/false setting meant to be edited over time in accordance with preference)
At the top of the code if it is a variable like a service or other static element
In the function(s) ONLY IF their use needs to be reset every time.
An example of #3 is a local script which has a function to resize a frame (from various available frames) based on certain things. The variable for the frame obviously has to be rewritten to the computer every time the function runs, or else the same frame would be resized forever even if another frame is intended.
I would actually be more wary of assigning service variables (among others in category #2 or #1) inside the functions because every time that function runs, you are effectively rewriting that variable even if it is not necessary. local PlayerGui = game:GetService("PlayerGui") will never be a variable that changes in my scripts—and so it goes at the top.