getgc
Returns a table of all objects tracked by the garbage collector.
Syntax
getgc(includeTables?: boolean) -> tableParameters
| Parameter | Type | Description |
|---|---|---|
includeTables | boolean? | Whether to include tables (default: false) |
Returns
| Type | Description |
|---|---|
table | Array of all GC-tracked objects |
Description
getgc returns all objects currently tracked by Luau's garbage collector. This is useful for finding specific functions, tables, or other objects in memory.
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" 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")Including Tables
-- Get all tables (slower, more results)
local allObjects = getgc(true)
for _, obj in ipairs(allObjects) do
if type(obj) == "table" and obj.SpecialKey then
print("Found target table!")
end
endNotes
- Without
includeTables, only functions and other non-table objects are returned - 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