filtergc

Filters objects in the garbage collector based on specified criteria.

Syntax

filtergc(type: "function" | "table", options: table, returnOne?: boolean) -> any? | {any}

Parameters

ParameterTypeDescription
typestringThe type of object to find ("function" or "table")
optionstableFilter options
returnOneboolean?Return the first match instead of an array (default: false)

Returns

TypeDescription
any? or tableThe first match when returnOne is true; otherwise an array of matches

Description

filtergc efficiently searches through garbage-collected objects with specific filter criteria. This is more performant than manually iterating through getgc().

Function Filter Options

OptionTypeDescription
NamestringMatch function name
ConstantstableMatch constants in function
UpvaluestableMatch upvalue values
HashstringMatch the value returned by getfunctionhash
IgnoreExecutorbooleanExclude Volt-created functions (default: true)
EnvironmenttableMatch the function's global environment
StartLinenumberMatch the function's starting source line
SourcestringMatch the function's short source name
UpvalueCountnumberMatch the exact upvalue count
ConstantCountnumberMatch the exact constant count

Table Filter Options

OptionTypeDescription
KeystableMatch table keys
ValuestableMatch table values
KeyValuePairstableMatch key-value pairs
MetatabletableMatch metatable

Example: Find Function by Name

local functions = filtergc("function", {
    Name = "targetFunction",
    IgnoreExecutor = false
})

for _, func in ipairs(functions) do
    print("Found:", func)
end

Example: Find Function by Constants

local functions = filtergc("function", {
    Constants = {"SomeUniqueString", "AnotherString"},
    IgnoreExecutor = false
})

Example: Find Table by Keys

local tables = filtergc("table", {
    Keys = {"Health", "MaxHealth", "Damage"}
})

for _, tbl in ipairs(tables) do
    print("Found table with game stats")
end

Example: Return the First Match

local function targetFunction()
    return "unique constant"
end

local match = filtergc("function", {
    Name = "targetFunction",
    IgnoreExecutor = false
}, true)

print(match == targetFunction) -- true

Notes

  • Function filters narrow the result: every supplied criterion must match
  • Constants, Upvalues, and Hash do not apply to C closures
  • getgc - Get all GC objects