Thread Tools Display Modes
04-29-17, 12:39 AM   #1
SuperRookie
A Deviate Faerie Dragon
Join Date: Apr 2017
Posts: 11
What happens to the local variable (with in a file) that is not used at all.

Let's say I have to following code in a lua file

Lua Code:
  1. local varA
  2. local varB = 0
  3. local varC = "stringA"
  4.  
  5. local function funcA()
  6.     local varD
  7.     local varE = 1
  8.     local varF = "stringB"
  9. end

I know that varD to varF are removed from CPU when the funcA is terminated.

But, what happens to varA to varC and funcA?

Do they still stay in memory although they are not called or used, or collected by garbage collection at EOF?
  Reply With Quote
04-29-17, 02:41 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
varA you have defined as nil in your example. varB and varC typically will not be garbage collected until your entire addon code is shut down or disabled, because some memory has been allocated to them as they hold values (0 and "stringA").

Lua will assume that varA, because it is nil, is no longer useful after some time, so the memory reserved for it will be collected. However, since varA is defined, even with a value of nil, its existance will continue until your code is shut down or disabled.

This is because Lua has no way of knowing if, in the future, what, if anything, you want to assign as a value to varA. Essentially, say 10 000 000 compute cycles later, you assign varA = true; if varA had been garbage collected, you would get an error saying varA no longer exists. Lua protects itself from that scenario.
  Reply With Quote
04-29-17, 02:44 AM   #3
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Oh, you also asked what happens to funcA. Same thing with varB and varC. Any memory used by processing funcA will be garbage collected, but funcA will stay defined so you can call it again later.
  Reply With Quote
04-29-17, 03:32 AM   #4
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I believe they will get garbage collected too, since there are no references to them. Not necessarily immediately, though.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
04-29-17, 05:49 AM   #5
SuperRookie
A Deviate Faerie Dragon
Join Date: Apr 2017
Posts: 11
Thanks for the info!

So, if I'd set those variables or functions to nil and run collectgarbage("collect"), would it wipe those from memory?
  Reply With Quote
04-29-17, 06:30 AM   #6
SuperRookie
A Deviate Faerie Dragon
Join Date: Apr 2017
Posts: 11
Seems it works?

Lua Code:
  1. print("|cffffff00table dones't exist, yet!|r", collectgarbage("count"));
  2.  
  3. local t = {};
  4.  
  5. print("|cffffff00table created!|r", collectgarbage("count"));
  6.  
  7. for i = 1, 10000 do
  8.     table.insert(t, i);
  9. end
  10.  
  11. print("|cffffff00elements inserted!|r", collectgarbage("count"));
  12.  
  13. t = nil;
  14.  
  15. print("|cffffff00table no longer exists!|r", collectgarbage("count"));
  16.  
  17. print("|cffffff00garbage collection on process!|r", collectgarbage("collect"));
  18.  
  19. print("|cffffff00garbage collected!|r", collectgarbage("count"));

But, fun fact is that there is slight increase on memory when table set to nil

Please refer to the attachment for result!
Attached Thumbnails
Click image for larger version

Name:	Cap 2017-04-29 22-28-43-733.jpg
Views:	127
Size:	30.7 KB
ID:	8936  
  Reply With Quote
04-29-17, 07:27 AM   #7
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Don't really know too much about Lua memory allocation and garbage collection. Possibly related to the print calls? Maybe the passed strings allocating new memory?
__________________
Grab your sword and fight the Horde!
  Reply With Quote
04-29-17, 07:56 AM   #8
SuperRookie
A Deviate Faerie Dragon
Join Date: Apr 2017
Posts: 11
That's an interesting point.

You might be right!
  Reply With Quote
04-29-17, 09:07 AM   #9
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Maybe this thread also helps

https://forums.wowace.com/showthread.php?t=18360
  Reply With Quote
04-29-17, 12:09 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
You do not need to call collectgarbage(). Forcing garbage collection is less efficient than letting it run normally. If it were the opposite, then something should be optimized with your code.
__________________
"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

  Reply With Quote
05-04-17, 01:26 AM   #11
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
Short explanation is this. Defined variables take up negligible space if any at all. Memory allocation in Lua is centered around values. Variables are just pointers to them in memory. Scoping is a process in which Lua isolates access to variables and the time in which they can be used. When the garbage collection runs, Lua looks for values hanging around in memory that have no variables pointing to them.

There are more complexities to Lua's memory management and each value type has their own specific rules. To provide a direct answer, variables defined in a scope that is still active won't randomly pop out of existence for any reason, even if they're set to nothing specific. Remember, nil is a value too even though its existence represents nothing.
__________________
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)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » What happens to the local variable (with in a file) that is not used at all.

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