getgc
Returns Luau functions tracked by the garbage collector, optionally including tables, userdata, and threads.
Syntax
getgc(includeTables?: boolean) -> tableParameters
| Parameter | Type | Description |
|---|---|---|
includeTables | boolean? | Whether to include tables (default: false) |
Returns
| Type | Description |
|---|---|
table | Array of functions, plus tables, userdata, and threads when requested |
Description
By default, getgc returns tracked functions. Pass true to also include tables, userdata, and threads.
Example
-- Get all functions in memory
for _, obj in ipairs(getgc()) do
if type(obj) == "function" then
print("Found function:", obj)
end
endFinding Specific Functions
-- Find a function by its constants
local function findFunction(targetString)
for _, obj in ipairs(getgc()) do
if type(obj) == "function" and islclosure(obj) then
local constants = debug.getconstants(obj)
for _, const in ipairs(constants) do
if const == targetString then
return obj
end
end
end
end
return nil
end
local targetFunc = findFunction("SomeUniqueString")
print(targetFunc or "No matching function was found")Including Tables
-- Get all tables (slower, more results)
local allObjects = getgc(true)
for _, obj in ipairs(allObjects) do
if type(obj) == "table" and rawget(obj, "SpecialKey") ~= nil then
print("Found target table!")
end
endNotes
- Without
includeTables, only functions are returned - With
includeTables, tables, userdata, and threads are added to the result - Including tables can be slow due to the large number of tables in memory
- Consider using
filtergcfor more targeted searches
Related Functions
filtergc- Filter objects with conditions