Memory Usage

Hello everyone, how are you doing?

I have another question:

With the code below, i realize that as i play, the amount of RAM increases as i use buffs, so far so good, that’s what should happen. And i also noticed that periodically the game itself recycles the use of memory by addons.

However, monitoring needtoknow and my addon, i was able to see that it “almost always” maintains a fixed amount of RAM, and even when it doesn’t, it recycles itself.

How is this possible?

I mean, besides clearing variables and setting scripts to null, what else would be important to do?

The code:

-- Configuração inicial para buffs específicos
local BuffsByClass = {
    ["DEMONHUNTER"] = {
        {ID = 258920, name = "Aura de Imolação", color = "Interface\\AddOns\\Cooldown\\Bar_Blue"},
        {ID = 427912, name = "Aura de Imolação", color = "Interface\\AddOns\\Cooldown\\Bar_Purple"},
        {ID = 427913, name = "Aura de Imolação", color = "Interface\\AddOns\\Cooldown\\Bar_Blue"},
        {ID = 390145, name = "Demônio Interior", color = "Interface\\AddOns\\Cooldown\\Bar_Ciano"},
        {ID = 162264, name = "Metamorfose", color = "Interface\\AddOns\\Cooldown\\Bar_Purple"},
    },
}

local playerClass = select(2, UnitClass("player"))
local activeBars = {}
local barPool = {}
local buffInfoCache = {}  -- Cache para informações de buffs

-- Posição inicial e espaçamento entre barras
local initialPosition = {"CENTER", UIParent, "CENTER", -450, -100}
local barSpacing = 32

-- Função para criar uma nova barra de status
local function CreateStatusBar()
    local frame = CreateFrame("Frame", nil, UIParent)
    frame:SetSize(200, 20)
    frame:Hide()

    local icon = frame:CreateTexture(nil, "BACKGROUND")
    icon:SetSize(24, 24)
    icon:SetPoint("LEFT", frame, "LEFT")
    
    local border = frame:CreateTexture(nil, "ARTWORK")
    border:SetSize(icon:GetWidth() + 10, icon:GetHeight() + 10)
    border:SetPoint("CENTER", icon)
    border:SetTexture("Interface\\AddOns\\Cooldown\\Borda")

    local statusBar = CreateFrame("StatusBar", nil, frame, "BackdropTemplate")
    statusBar:SetSize(160, 14)
    statusBar:SetPoint("LEFT", icon, "RIGHT", 5, 0)
    statusBar:SetBackdrop({
        bgFile = "Interface\\AddOns\\Cooldown\\Fundo",
        tile = true,
        tileSize = 32,
        insets = {left = -1, right = -1, top = -1, bottom = -1},
    })

    local timerText = statusBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    timerText:SetPoint("RIGHT", statusBar)
    timerText:SetFont("Interface\\AddOns\\Pulso\\Orbitron.ttf", 9)

    local nameText = statusBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetPoint("LEFT", statusBar, 4, 0)
    nameText:SetFont("Interface\\AddOns\\Fontes\\Prototype.ttf", 8)

    frame.icon = icon
    frame.statusBar = statusBar
    frame.timerText = timerText
    frame.nameText = nameText

    return frame
end

-- Função para obter ou criar uma nova barra do pool
local function GetStatusBar()
    if #barPool > 0 then
        return table.remove(barPool)
    else
        return CreateStatusBar()
    end
end

-- Função para obter informações de buff, usando cache
local function GetBuffInfo(buffID)
    if not buffInfoCache[buffID] then
        local spellInfo = C_Spell.GetSpellInfo(buffID)
        buffInfoCache[buffID] = {iconID = spellInfo.iconID, name = spellInfo.name}
    end
    return buffInfoCache[buffID]
end

-- Função para configurar e mostrar a barra do buff
local function ShowBuffBar(buffData, duration, expirationTime)
    local frame = GetStatusBar()
    local buffInfo = GetBuffInfo(buffData.ID)  -- Usando cache
    frame.icon:SetTexture(buffInfo.iconID)  -- Obtendo o ícone pelo ID da spell
    frame.statusBar:SetStatusBarTexture(buffData.color)
    frame.nameText:SetText(buffData.name)

    -- Define a posição da barra com base na quantidade de barras ativas
    local yOffset = (#activeBars * barSpacing)
    frame:SetPoint(initialPosition[1], initialPosition[2], initialPosition[3], initialPosition[4], initialPosition[5] + yOffset)

    frame.statusBar:SetMinMaxValues(0, duration)
    frame.statusBar:SetValue(duration)
    frame.expirationTime = expirationTime

    frame:Show()
    table.insert(activeBars, frame)
end

-- Atualização centralizada de todas as barras ativas
local function UpdateAllBuffs(elapsed)
    local currentTime = GetTime()
    for i = #activeBars, 1, -1 do
        local frame = activeBars[i]
        local timeRemaining = frame.expirationTime - currentTime

        if timeRemaining <= 0 then
            frame:Hide()
            table.remove(activeBars, i)
            table.insert(barPool, frame)
        else
            frame.statusBar:SetValue(timeRemaining)
            frame.timerText:SetText(math.floor(timeRemaining))  -- Exibindo o tempo em segundos inteiros
        end
    end
end

-- Função para detectar buffs específicos e mostrar barras
local function OnEvent(_, event, unit, info)
    if unit == "player" and info.addedAuras and BuffsByClass[playerClass] then
        for _, aura in pairs(info.addedAuras) do
            for _, buffData in ipairs(BuffsByClass[playerClass]) do
                if buffData.ID == aura.spellId and aura.expirationTime then
                    ShowBuffBar(buffData, aura.duration, aura.expirationTime)
                end
            end
        end
    end
end

-- Inicializa o frame e eventos
local frame = CreateFrame("Frame")
frame:RegisterEvent("UNIT_AURA")
frame:SetScript("OnEvent", OnEvent)
frame:SetScript("OnUpdate", UpdateAllBuffs)