Thread Tools Display Modes
08-08-07, 12:21 PM   #1
philoubmx
A Murloc Raider
Join Date: Aug 2007
Posts: 6
Unhappy Memory optimization with UnitName, UnitMana ...

Hi everybody;

First, i would like that i'm french, so sorry for my poor english

I'm developping my own addon for raid management. I'have installed RessourceTool in order to check how many memory is used by my addon.
And i notice a strange behavior : when i use function like "UnitMana", "Unithealth", Unitxxx; the memory increased until the Garbage Collector do his job.

When i dont use Unitxxx, the memory addon is constant (about 100ko), when i use Unitxxx, the memory grows (about 500ko) and the descrease to 100kon, and then grows...etc

Somebody already notice it and know what to do ?
  Reply With Quote
08-08-07, 12:46 PM   #2
Layrajha
A Frostmaul Preserver
 
Layrajha's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 275
Can you show the part of the code that calls that? It's probably more about how you used those functions that about the functions themselves.

(Sorry about that, but I just have to: C'est rare de voir un compatriote sur ce site, Salut ^^)
  Reply With Quote
08-08-07, 01:07 PM   #3
philoubmx
A Murloc Raider
Join Date: Aug 2007
Posts: 6
Hi,

This this the function i use :

function My_UnitMana(unitid)
return UnitMana(unitid) or "";
end

...
And i call it like this :

table.insert(tmpmess, tostring(My_UnitMana(unit)));


If i replace my function by this (for test):
function My_UnitMana(unitid)
--return UnitMana(unitid) or "";
return 1;
end

the memory is constant.

I understand that Unitxxx use memory, but i dont understand why it growing ...

(Petite parenthese : salut l'ami ! en esperant que mon anglais ne soit pas trop mauvais ! Je prefere repondre en anglais histoire de respecter la langue du forum )

Last edited by philoubmx : 08-08-07 at 02:16 PM.
  Reply With Quote
08-08-07, 02:15 PM   #4
philoubmx
A Murloc Raider
Join Date: Aug 2007
Posts: 6
Forget my post ...
Its not Unitxxx which is the problem.

I try with

function My_UnitMana(unitid)
--return UnitMana(unitid) or "";
return 9999;
end

and i have the same problem.

As you said, it how i manage my variable ,) Im gonna check this way
  Reply With Quote
08-08-07, 02:33 PM   #5
Layrajha
A Frostmaul Preserver
 
Layrajha's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 275
Well what takes space is your table. You are inserting things in a table, so unless you remove that afterward, the table will grow. Let's say your are calling your function on a mage that is DPSing randomly: the table after the mage has stopped will look like:
{9500,9250,9000,8750,8500,....,500,250,0}
Now, if you were also targeting other people from time to time, their data would be added too.
Why do you want to do with those values? Cause actually, maybe you're doing something nice with them, but if you don't remove them from time to time, you'll end up with a {0,1,2,3,4,5,6,7,8,9,...,12000} table which won't be of a great help for anything
  Reply With Quote
08-08-07, 03:05 PM   #6
philoubmx
A Murloc Raider
Join Date: Aug 2007
Posts: 6
Thanks for your help.

In fact, i see that string concatenation take lot of memory and that is more usefull on my case to use table and then concat it.

Exemple :
tmp = "";
for i=1; 40; do
tmp = tmp .. "1"..a.."2"..b.."3"..c.."4"..d..<......> .."40"... ; (for exampl)
end


It seems to take lot memory so i use a table.
I found code on the ACE2 website (http://www.wowace.com/wiki/Coding_Tips)

local cache = setmetatable({}, {__mode='k'})
data= next(cache) or {};
table.insert( data, < value > )
...
for j in pairs( data) do
data[j] = nil -- this section clears the temporary table
end
cache[data] = true



So i change my code to (but im really not sure i choose the best way ...):
local cache = setmetatable({}, {__mode='k'})
data= next(cache) or {};
for i=1;40; do
table.insert( data, tostring(i) );
table.insert'(data, tostrinf(My_UnitMana("raid"..k)));
end
print( table.concat(data, ""));
for j in pairs( data) do
data[j] = nil -- this section clears the temporary table
end
cache[data] = true



(This a simple exampl)

Its the first time i'm developping an addon but i would like it dont take to much memory, that why i would to optimiz it; and i saw that "string" can be optimized... so i try

Last edited by philoubmx : 08-08-07 at 03:08 PM.
  Reply With Quote
08-08-07, 03:31 PM   #7
Layrajha
A Frostmaul Preserver
 
Layrajha's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 275
I think that the part that doesn't work is the one that clears the table. I also think that it's not really needed to have both the use of a metatable-cache via the "mode='k'" and a manual clear.

Here, a simple table will do just fine, I think. Start with
Code:
local data = {}
for i = 1,40 ....
And to clear the table, I would use
Code:
for key,_ in ipairs(data) do
  data[key]=nil
end
I'm not sure if it's needed, I'm not as pro as I could be in lua and I don't know how the garbage collector treats local variable, how their scope is handled exactly, etc...

If it still does the same, well, I just said a whole bunch of crap, sorry :/
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Memory optimization with UnitName, UnitMana ...


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