Reply
Thread Tools Display Modes
Unread 03-22-13, 10:26 AM   #1
Benalish
A Fallenroot Satyr
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 28
Unusual memory usage

In my addon I noticed an unusual use of memory ranges from 200 kb to 1.10 mb. After several tests, I found the cause of this oscillation, located inside a function. I noticed that disabling (commenting) the other lines and leaving only this, the oscillation is still present. This function is called every OnUptate, and setting a refresh rate relatively high (about 50 sec) memory usage becomes stable.
I do not write the whole code because the source problem is only here

Lua Code:
  1. function buffscan()
  2.         -- lines disabled
  3.     local meow = { "a","b","c","d","e","f","g","h" }
  4.         -- lines disabled
  5. end
  6.  
  7. local timer = 0
  8.  
  9. local function onUpdate(self,elapsed)
  10.     timer = timer + elapsed
  11.     while (timer >= 0.75) do
  12.         if (not db.profile["Locked"]) then
  13.             for x, key in ipairs(db.profile["Bars"]) do
  14.                 for y = 1, db.profile.Bars[x].columns * db.profile.Bars[x].rows do
  15.                     buffscan()
  16.                 end
  17.             end
  18.         end
  19.         timer = timer - elapsed
  20.     end
  21. end
  22.  
  23. local f = CreateFrame("frame")
  24. f:SetScript("OnUpdate", onUpdate)
Benalish is offline   Reply With Quote
Unread 03-22-13, 10:59 AM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,191
You're creating a new meow table each time the function runs

If the contents of meow are static, then just move it's definition above/out of buffscan

If they're not static, then still move it above and use table.wipe(meow) inside the function to clear it and put new values.
Dridzt is offline   Reply With Quote
Unread 03-22-13, 11:19 AM   #3
Benalish
A Fallenroot Satyr
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 28
I tried this

Lua Code:
  1. local meow
  2.  
  3. function buffscan()
  4.         -- lines disabled
  5.      meow = { "a","b","c","d","e","f","g","h" }
  6.         -- more stuff
  7.     table.wipe(meow)
  8. end

and the problem is still present
Benalish is offline   Reply With Quote
Unread 03-22-13, 11:41 AM   #4
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,191
I meant this if meow is a static table
Code:
local meow = { "a","b","c","d","e","f","g","h" }
     
function buffscan()
    -- do something with meow
end
or this
Code:
local meow = {}
function buffscan()
    wipe(meow)
    -- do something with meow
end
if you need to clear it and put new values in it.

You know what :P Post your whole code or a link to a pastey with it.
It's hard to give advise when we don't know what you're trying to do, regardless if the problem is obvious.

Last edited by Dridzt : 03-22-13 at 11:44 AM.
Dridzt is offline   Reply With Quote
Unread 03-22-13, 12:06 PM   #5
Benalish
A Fallenroot Satyr
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 28
Is a DYNAMIC table

I write an example of how it should work my script

Lua Code:
  1. local foobar = {
  2.     [1] = "Welcome#To#My#World",
  3.     [2] = "Secret#To#The#End",
  4.     [3] = "My#Little#Universe",
  5. }
  6.  
  7. function buffscan(x)
  8.          local meow = { strsplit("#", foobar[x] }
  9.         -- do something
  10. end
  11.  
  12. local timer = 0
  13.  
  14. local function onUpdate(self,elapsed)
  15.     timer = timer + elapsed
  16.     while (timer >= 0.75) do
  17.         for x, key in ipairs(foobar) do
  18.             buffscan(x)
  19.         end
  20.         timer = timer - elapsed
  21.     end
  22. end
  23.  
  24. local f = CreateFrame("frame")
  25. f:SetScript("OnUpdate", onUpdate)
Benalish is offline   Reply With Quote
Unread 03-22-13, 12:19 PM   #6
Kendian
A Molten Giant
 
Kendian's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 614
Originally Posted by Dridzt View Post
You're creating a new meow table each time the function runs

If the contents of meow are static, then just move it's definition above/out of buffscan

If they're not static, then still move it above and use table.wipe(meow) inside the function to clear it and put new values.
Off topic: I thought I was in SuperTroopers, for a moment~
__________________
Kendian is offline   Reply With Quote
Unread 03-22-13, 01:03 PM   #7
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,191
Why are you being so difficult?

Example code doesn't help me because I still don't have the slightest what you're trying to actually do.
For that reason I can't suggest a re-structuring of your tables so you don't need to do 2 costly operations in an onupdate (strplit and creating a new table each time).

What are the keys supposed to be? Why can't you make it a hash table?

Sorry, but I'm stepping out of the thread for the moment, hopefully someone else can help.
(it shouldn't be like pulling teeth to get to your actual code, is it a secret?)
Dridzt is offline   Reply With Quote
Unread 03-22-13, 01:13 PM   #8
Benalish
A Fallenroot Satyr
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 28
As you prefer

*SPOILER* It's very very very long, can result difficult for you to read whole

http://nopaste.dk/p23585, line 373, 380 e 504
Benalish is offline   Reply With Quote
Unread 03-22-13, 01:17 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 9,597
That's not "very very very long".

/edit: unless it's very much longer than it should be for what it does - I admittedly didn't really read through it.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

Seerah is offline   Reply With Quote
Unread 03-23-13, 01:32 AM   #10
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,011
In this post, I'll be referring to the actual code in use as linked.

Since the table tracks is registered as an upvalue already, I'll address a solution to assigning a series of values to it in the function. The way it is set up now, it's going to create a new table every time the function runs. The brackets used to define the table are actually called the Table Constructor and their entire purpose is to build a new table with the values given inserted initially.

What I suggest is to replace line 380 with the following:
Lua Code:
  1. table.wipe(tracks);
  2. for i in string.gmatch(db.profile["Bars"][x][y].track,"[^#]+") do tinsert(tracks,i); end

What this does is runs a pattern-matching iterator to reconstruct the table in the same manner strsplit() would, reusing the old table instead of creating a new one each time. The pattern string I provided tells it to return chunks of the string that doesn't include the hash (#) symbol.

For more information (official Lua documentation):
String Functions - http://www.lua.org/manual/5.1/manual.html#5.4
Patterns - http://www.lua.org/manual/5.1/manual.html#5.4.1
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is offline   Reply With Quote
Unread 03-24-13, 07:06 AM   #11
Benalish
A Fallenroot Satyr
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 28
Done, but the anomalies in memory usage persist
I also made ​​substantial changes
http://nopaste.dk/p23706
Benalish is offline   Reply With Quote
Unread 03-24-13, 09:09 AM   #12
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 548
Not sure why nobody mentions garbage collect. But your anomalie is normal behaviour. Unused values, tables or functions are not removed from memory instantly. There is a process running in the background that removes the data after a while. Its called garbage collect. So for a while your memory usage increases but will drop after the next run of garbage collect.
You can force a clean up with
Lua Code:
  1. collectgarbage("collect")

http://wowprogramming.com/docs/api/collectgarbage
__________________
The cataclysm broke the world ... and the pandas could not fix it!
Rilgamon is offline   Reply With Quote
Unread 03-24-13, 10:13 AM   #13
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,011
In short, garbage accumulation and collection is a normal part of running code in Lua. However, forcing a full collection OnUpdate is taxing on any system especially at times when as much CPU power as possible is needed. This is why it's usually best to use preventative measures to ensure not as much garbage is generated too fast. A handful of kilobytes a second is normal, but if you're going through hundreds of them a second, there's a little cause for concern.
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is offline   Reply With Quote
Unread 03-24-13, 11:15 AM   #14
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,051
If you find yourself needing to use collectgarbage(), chances are you're doing something very wrong.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of Revelation, Spamalyzer, TravelAgent, Volumizer, and many other AddOns.
Torhal is offline   Reply With Quote
Unread 03-24-13, 11:20 AM   #15
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 548
Thats why I used the word "force"
__________________
The cataclysm broke the world ... and the pandas could not fix it!
Rilgamon is offline   Reply With Quote
Unread 03-25-13, 11:49 AM   #16
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,011
Reiterating
Originally Posted by SDPhantom View Post
However, forcing a full collection OnUpdate is taxing on any system especially at times when as much CPU power as possible is needed.
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is offline   Reply With Quote
Unread 03-28-13, 11:37 AM   #17
Resike
A Molten Giant
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 594
Originally Posted by Benalish View Post
As you prefer

*SPOILER* It's very very very long, can result difficult for you to read whole

http://nopaste.dk/p23585, line 373, 380 e 504
I think admins here can deal with 500 line codes. :P
Resike is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » Unusual memory usage

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