Thread Tools Display Modes
04-05-22, 09:37 AM   #1
solidity0815
A Defias Bandit
Join Date: Apr 2022
Posts: 3
High memory usage after login

Hey there, i've recently started writing my first "real" addon (as in, it provides actual regularly used functionality). It's basically a log of all crafts by the player that provides extra information that I use with TSM.

The addon is using only slightly more than 1MB of memory atm, except on login, where it spikes to 60-120MB, before being culled by garbage collection.

Is there some way to find out what is hogging all that memory?

I'm not new to programming in general, but new to WoW addon programming, so please be gentle
  Reply With Quote
04-05-22, 11:22 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
Without being able to review code no one will be able to answer this

Could be related to saved variables, could be you're caching something when the addon is starting up but you need to show your code.
  Reply With Quote
04-05-22, 11:24 AM   #3
solidity0815
A Defias Bandit
Join Date: Apr 2022
Posts: 3
Sure thing, it's hosted on github here
  Reply With Quote
04-05-22, 01:35 PM   #4
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
The first argument in the ADDON_LOADED payload is the name of the addon that just loaded, equal to the filename of the toc which is also passed to every file loaded within an addon. You are not checking for that, so your addon is repeating your initialize and setup functions dozens of times for every addon that loads.

At the top of main.lua, add the following:

Code:
local addonName = ...
Then compare the first argument of ADDON_LOADED to addonName

Last edited by Kanegasi : 04-05-22 at 01:38 PM.
  Reply With Quote
04-05-22, 02:28 PM   #5
solidity0815
A Defias Bandit
Join Date: Apr 2022
Posts: 3
That wasn't it, but it sent me on the right path.

main.lua is no longer in use, and I have removed it from github.

But adding loads of debug-prints made me aware that my function GetItemStats (which parses and consolidates the SavedVariables table) was being called lots on load. Turns out GetItemStats is a builtin function of WoW which I overwrote...

After a small namechange for the function, the addon stays @ ~1.5MB.

Thx for your help
  Reply With Quote
04-06-22, 09:51 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
It's good practice to make local functions instead of global ones whenever you can. It'll help you avoid name collisions like these in the global table.

As with any local variable, you need to define the variable before you reference it elsewhere, even if you have to temporarily leave it nil. Otherwise, Lua will try to look for it in the global table instead.



Option 1: Define the function before any reference you make to it.
Lua Code:
  1. local function MyFunction()
  2. --  Do stuff
  3. end
  4.  
  5. --  Other functions that call MyFunction() should be placed after it

Option 2: Mark the function name as a local at the top of your Lua file.
Lua Code:
  1. local MyFunction;-- Function "prototype"
  2.  
  3. --  You can place your functions in any order you want as long as they appear after the "local" statement above
  4.  
  5. function MyFunction()-- Lua will see this name is defined as a local at the top of the file and will place it in that scope instead of the global table
  6. --  Do stuff
  7. end



Option 2 is an easier way to convert existing code to use locals, but I consider Option 1 to be cleaner and should be considered when writing new code.

PS: Keep in mind your addon shares the global table with all other addons too. It's suggested for every global you use (including frame and region names) are prefixed with the name of your addon to also help prevent name collisions.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 04-06-22 at 09:55 AM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » High memory usage after login

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off