How does memory management work in roblox?

hey.
I need some help.

I really want to figure out how to make my scripts more memory efficient. I know lua has this garbage collection system, i know that. But what I don’t know is… the rest.

please if anybody can give me some tips, i’d appreciate it.

There isn’t much too it I guess, some simple tips I could give are things like

Object Pooling - Creating Lots Of Instances On The Server (Try do this on the Client/s)
Using Local Variables
Clearing Tables (Instead Of Making New Ones)

Another One You Can Do But I Wouldn’t Recommend Most Of The Time Is Disconnecting Event Listeners
Example :

local connection
connection = game:GetService("RunService").RenderStepped:Connect(function()
    if not condition then
        connection:Disconnect()
        connection = nil
    end
end)

Use do end to create a local scope (visibility zone). Once variables within this scope are no longer in use, the garbage collector will detect and clean them up. Additionally, use buffers if you only need to cache data without requiring the functionality of tables.

Here’s an example code that stores direction vectors for a sphere:

local VECTOR_SIZE = 12 -- Size of a single vector in bytes (3 floats, 4 bytes each)
local AXIS_SIZE = 4    -- Size of one float in bytes

local VecCount = 10    -- Number of points on one axis
local SphereVector = buffer.create(VecCount^2 * VECTOR_SIZE) -- Create a buffer to store vectors

local pi2 = math.pi * 2
local cos = math.cos
local sin = math.sin

do -- Start of the local scope (visibility zone)
  
  local increment = pi2 / VecCount -- Angle increment for each step
  local step = 0 -- Index for writing to the buffer

  -- Generate sphere vectors
  for xAngle = increment, pi2, increment do
    for yAngle = increment, pi2, increment do
      local x = cos(xAngle) * sin(yAngle)
      local y = sin(xAngle) * sin(yAngle)
      local z = cos(yAngle)
      
      -- Write the vector components to the buffer
      buffer.writef32(SphereVector, step * VECTOR_SIZE, x)         -- Write x
      buffer.writef32(SphereVector, step * VECTOR_SIZE + 4, y)     -- Write y
      buffer.writef32(SphereVector, step * VECTOR_SIZE + 8, z)    -- Write z
      
      step += 1 -- Move to the next position in the buffer
    end
  end

end -- End of the local scope; garbage collector will detect and clean up unused variables

Not disconnecting events may lead to a memory leaks, which are a lot more dangerous than using fraction of pretty much large server memory

Clearing tables do the same, they gain you little to no improvement, because it’s micro-optimization

1 Like

Memory isn’t your main problem, you should aim to optimize your scripts, and this is specific to every system, there aren’t any universal tips like do that to make your game 2x better

Code performance come from different categories, but main 3 ones are:

  • stress
  • tasks
  • data

Let’s examine them

  1. Stress is how often you run your code, the less often is better, as it gives some time for the server to rest until next operation

  2. Tasks mean how memory intesive is given piece of code in given time period, usually a frame, this is why using better algorithms or splitting code across the frames might help improve performance a lot

  3. Data is how much stuff your script is storing, referencing or creating, having large quantities of parts or connections, storing a lot of tables or using thousands of threads at once might cause memory leaks or use your memory you need to store this data, You should always look for better ways to store data, and avoid it when not neccesary

This is practically all you need to know to make your scripts more efficient, but really, if your game doesn’t lag, don’t optimize your scripts, try and measure

1 Like