Jump to content

Lua collectgarbage() every frame


Josh
 Share

Recommended Posts

This demonstrates that you can call collectgarbage() every frame in Lua with no effect on the framerate:

-- Define a function that performs intensive calculations and variable allocation
function testGarbageCollection()
    local iterations = 1 -- Adjust the number of iterations as needed
    
    -- Perform calculations and variable allocation in a loop
    for i = 1, iterations do
        -- Intensive calculations (e.g., math operations)
        local result = math.sqrt(i) * math.sin(i) * math.cos(i)
        
        -- Allocate tables with random data
        local table1 = {}
        for j = 1, 10 do
            table1[j] = math.random()
        end
        
        -- Create temporary strings
        local str = ""
        for j = 1, 10 do
            str = str .. tostring(math.random())
        end
        
        -- Let's intentionally create some cyclic references
        local cyclicTable = {}
        cyclicTable.circularRef = cyclicTable

        -- Simulate resource-intensive operations
        local resourceIntensiveData = {}
        for j = 1, 10 do
            resourceIntensiveData[j] = {}
            for k = 1, 10 do
                resourceIntensiveData[j][k] = math.random()
            end
        end
    end
    
    return "Test completed" -- Return a message when the test is done
end

-- Call the function to trigger the test
print(testGarbageCollection())


--Get the displays
local displays = GetDisplays()

-- Create a window
local window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[1], WINDOW_CENTER | WINDOW_TITLEBAR)

-- Create a world
local world = CreateWorld()

-- Create a framebuffer
local framebuffer = CreateFramebuffer(window)

-- Create a camera
local camera = CreateCamera(world)
camera:SetClearColor(0.125)
camera:SetPosition(0, 0, -4)

-- Create a light
local light = CreateBoxLight(world)
light:SetRotation(45, 35, 0)
light:SetRange(-10, 10)
light:SetColor(2)

-- Create a model
local model = CreateBox(world)
model:SetColor(0, 0, 1)

-- Main loop
while not window:Closed() and not window:KeyDown(KEY_ESCAPE) do
    model:Turn(0, 1, 0)
    world:Update()
    world:Render(framebuffer, false)
    testGarbageCollection()
    if window:KeyDown(KEY_SPACE) then collectgarbage() end
end

 

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...