It looks like you're new here. If you want to get involved, click one of these buttons!
-- Example Char = Core.class(Sprite) local boy = Char.new() local girl = Char.new() -- Later how can I get an array containing boy & girl objects, from Char? (Let's pretend I have no way to store it manually) -- Something like : local charArray = Char:getInstances() |
Comments
Likes: Mells, atilim
However you need to be aware that as you now have an extra reference to each object they won't ever be garbage collected unless you explicitly nil out the references - you might be better off making a pool of objects at the start of your game and then having a flag in the object to say which entries in the pool are currently in use.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
1. When does the garbage collector do the work for me?
Also I have another question :
2. What happens in the following case?
In your note, do references apply to instances only or also to their childs?
When an object is referenced, does it mean a reference is created for their child also?
Thanks
In the case of your first code, as soon as i and j go out of scope (ie the block or closure they're declared in ends) the space allocated to them will be available for collection.
Likewise if they are references to tables or other structures then THAT memory will still remain allocated whilst a reference to it exists - a good idea is always to explicitly set the reference to "nil" when you've finished with it.
When the reference variable goes out of scope (and is available to be collected) the memory it's pointing to is also considered "free" and so under "normal" circumstances the whole process will manage itself - eventually! Setting the reference variables to "nil" just gives the collector an extra "heads up" as to what's free and what's not.
You can't predict exactly when the GC will run - it's run as a background process periodically, however you can force a call (or two) using the collectGarbage("collect") function.
In all "managed" apps, dynamic memory allocation is quite an expensive process (in terms of processing power / bandwidth used) and should be done sparingly - for instance NEVER allocate locals inside for loops as a new var will be allocated each time round the loop and you'll get a massive memory hit and then the GC will have a lot of work to do (and can often cause stutter in your game).
It's my understanding that ANY and ALL variable declarations, functions, closures, tables etc all go through the managed memory system and will eventually get "collected" as soon as they're available.
Again my understanding is that "references" are just pointers (in the C/C++ sense) when you "store" a reference to a block of memory it's reference counter goes up and while the counter > 1 then it'll never get collected.
In your second example (assuming both boy and girl are Sprite's - actually a table) then a reference to boy1 and girl1 are stored which will prevent them (or anything they reference) from being collected until boy and girl themselves are eligible for collection.
Hope this helps - let me know if there's anything else you'd like me to clarify.
Jon...
Likes: Mells
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Does your note apply in that case or is it different?
Side note to myself : this community is amazing
In a trivial example like you've specified there it might be easier to write it like this...
Actually - come to think of it (and not having looked through the lua source or actually asked a lua expert that has), just declaring a local like that inside a function might be the equivalent of declaring a local in C (ie it just reserves space on the stack) and so it might be a moot point after all.
The best way to be sure once and for all would be to put the code in a big ass loop and then print out value returned from the garbage collector (watching that the print statement doesn't also allocate memory as well)
ie.
Likes: Mells
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Also strings are hashed and stored uniquely in memory. Therefore these 3 strings are stored at the same memory location:
Likes: Mells
*edit* Doh!! Ninja'd twice
Likes: Mells
Website: http://www.castlegateinteractive.com
https://play.google.com/store/apps/developer?id=Castlegate+Interactive
*sheesh* and they said memory management in C++ was hard!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
@techdojo
I know I ask a lot today sorry, I'm just a few weeks from the beginning of my learning (even though I had done a bit of programming with another framework, I had to stop soon) so please bear with me while I'm learning. Yes, I was thinking about something more complex.
I was trying to avoid putting too many locals outside of my functions for example :
@atilim > Interesting, I hadn't come across that information yet (lua-users and other sites that I've read the last few weeks).
@Scouser > If I understand well you mean that name will loose scope and so be collected. But on the other hand it doesn't help if what we want to avoid is recreating this local for each loop. Do I get it well?
Personally, I'm always ready to help anyone who's prepared to help themselves. What I hate is forums where some noob just pops up and say's "waaa this is too hard, will someone do my homework for me", making the effort to post a question, ask intelligent follow up questions and being prepared to experiment is more than enough for me to get involved (if I can)
Likes: Mells, atilim
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
As a side note, the code: