So I understand that Lua uses reference counts for objects to determine when to truly garbage collect objects with no references but I have a quick question regarding local variables.
Suppose I have this code:
local newX
local function changeX(x)
local dx = math.random(0, 20)
x = x + dx
return x
end
newX = changeX(5) |
My question is, will I have a memory leak if I run this function say 1000 times without niling "dx"?
I don't mind niling these variables each time, but I'm worried that unnecessarily niling variables might cause slight performance drops for similar functions that I might be calling 100+ times a frame.
Comments
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
Say I have a Gideros class in a Lua file and I wish to have some "private static" Lua variables using local variables in the Lua file. Assuming I get rid of all Gideros Sprites properly (remove from parent, nil, etc), when would all the local vars in the file such as numbers, strings, and lua tables be queued for gc? Perhaps when I remove the packages.loaded["luafilename"] entry?
In case that confused you, here is a clear version of my question:
For local file variables, when do I and when do I need to nil them for them to be gc'd (Assuming no references are left other than the local variable)
a) numbers/string
b) functions (Assume cleared from EventDispatchers [No references other than local var])
c) tables
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
1) you exit an app
2) you unload the require file
But to unload the required filem nillin packages.loaded["luafilename"] might not be enough
check out this snippet:
http://snippets.luacode.org/snippets/unrequire_121
a={1,2,3}
b=a
this does not make a copy of a into b, rather a and b are now pointing to the same patch of memory. If you set a and b to something else (not necessarily nil) (eg a=5; b="fred") then the table {1,2,3} is elligible for garbage collection. The variables a and b are not garbage collected: they don't need to be. Variables (local or global) never cause memory leaks, it is the anonymous memory locations they point to that could cause memory leaks.
When using Gideros, remember that hte Gideros system also holds refernces to memory areas so even if you nil out all your lua variables the memory might not be collected. An obvious example is a sprite where you have no refernces to it but the sprite is still on screen since Gideros is keeping track of it. You will have to removeFromParent as well to get rid of it.
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
https://play.google.com/store/apps/developer?id=Into-It+Games
http://appstore.com/LidiaMaximova
a={1,2,3}
a="foo"
after the second command the table {1,2,3} is ready for garbage collection. Note that Lua can handle circular references and this does not prevent garbage collection
a={}
b={}
a.next=b
b.next=a
a=nil
b=nil
now, both a and b can be GC'd.
Also
parent={1,2,3}
parent.child={"a","b","c"}
child.parent=parent
parent=nil
now both tables are garbage collected.
Likes: ar2rsawseen
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975