WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Unusual memory usage (https://www.wowinterface.com/forums/showthread.php?t=46093)

Benalish 03-22-13 10:26 AM

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)

Dridzt 03-22-13 10:59 AM

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.

Benalish 03-22-13 11:19 AM

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 :o

Dridzt 03-22-13 11:41 AM

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.

Benalish 03-22-13 12:06 PM

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)

Kendian 03-22-13 12:19 PM

Quote:

Originally Posted by Dridzt (Post 275041)
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~ :D

Dridzt 03-22-13 01:03 PM

Why are you being so difficult? :p

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. :o
(it shouldn't be like pulling teeth to get to your actual code, is it a secret?)

Benalish 03-22-13 01:13 PM

As you prefer :D

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

http://nopaste.dk/p23585, line 373, 380 e 504

Seerah 03-22-13 01:17 PM

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. :p

SDPhantom 03-23-13 01:32 AM

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

Benalish 03-24-13 07:06 AM

Done, but the anomalies in memory usage persist :(
I also made ​​substantial changes
http://nopaste.dk/p23706

Rilgamon 03-24-13 09:09 AM

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

SDPhantom 03-24-13 10:13 AM

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.

Torhal 03-24-13 11:15 AM

If you find yourself needing to use collectgarbage(), chances are you're doing something very wrong.

Rilgamon 03-24-13 11:20 AM

Thats why I used the word "force" ;)

SDPhantom 03-25-13 11:49 AM

Reiterating :rolleyes:
Quote:

Originally Posted by SDPhantom (Post 275130)
However, forcing a full collection OnUpdate is taxing on any system especially at times when as much CPU power as possible is needed.


Resike 03-28-13 11:37 AM

Quote:

Originally Posted by Benalish (Post 275053)
As you prefer :D

*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


All times are GMT -6. The time now is 07:08 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI