Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Confusion about garbage collection — Gideros Forum

Confusion about garbage collection

shsngshsng Member
edited May 2012 in General questions
Being new to Gideros and Lua, I have always thought that there is automatic garbage collection (similar to Java), so I do not ever have to call collectgarbage() in my code. I found out, however, that my app was leaking memory. When I tracked memory usage by calling collectgarbage("count") in an EnterFrame event handler, I saw that memory usage was building up and never freed. I added an explicit collectgarbage() call in a timer loop periodically and the memory leak went away. Am I doing something wrong?

Also, when I tested calling collectgarbage() inside EnterFrame event handler, my movieclip animations stopped working. Once I removed the collectgarbage() call, my animations started playing again. Is this expected behavior? Why would collectgarbage() affect movie clips?

I still have a lot to learn. :)

Dislikes: Yan

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Well, from my perspective I've never used collectgarbage (well only for testing purpose sometimes) and didn't have any issues for now.

    Maybe problem is that memory is not freed right away, only after some time?
  • shsngshsng Member
    @ar2rsawseen: Thanks for your comment. You were right; the garbage collection was happening automatically at a slower rate than I was expecting. Memory usage dropped after a while. I also found out why my movieclips stopped when I called collectgarbage(). My movieclips were assigned to local variables inside a function, and those variables were destroyed by the garbage collector when I explicitly called collectgarbage(). Now it all made sense to me again. :)
  • The thing to remember about Garbage collection is Lua is that nothing will be marked as "garbage" whilst there is still a reference to it. Normally local variables will disappear as soon as the block they are declared in finishes (however you need to be aware of "closures" and how they affect local variables).

    If you use class level or global variables as references then the item being referenced will still remain referenced until those globals are assigned new values - often this means that a lot of data is kept "hanging around" even though it's been finished with as it's technically still referenced.

    If you want to be totally obsessive about your garbage it's not a bad idea to explicitly set class or global variables to a nil value as soon as you've finished with the data as that gives the GC a better chance to know what's actually garbage.

    Just my $0.02
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • evsevs Member
    I NEVER call the garbage collector in case it collects my whole app :))
    +1 -1 (+2 / -0 )Share on Facebook
  • Ironically I'm MORE careful about memory usage, leaks and the GC when using languages like Lua, Javascript and Java than I am with C/C++
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • atilimatilim Maintainer
    Ironically I'm MORE careful about memory usage, leaks and the GC when using languages like Lua, Javascript and Java than I am with C/C++
    so true... :)
  • atilimatilim Maintainer
    edited May 2012
    On the other hand, I have some simple rules about managing memory:

    1. Try to allocate small number of objects/tables, preferably reuse them.
    2. Call collectgarbage() at the beginning of a level, at the end of a level and at the pause menu.
    3. Also, on iOS, we automatically call collectgarbage() (twice) when there is a memory warning.

    Likes: techdojo, avo

    +1 -1 (+2 / -0 )Share on Facebook
  • Very good advice from @atilim, reusing pools of tables is a much cleaner approach and is something I'd advise as well.

    In my current framework - I actually call collectgarbage() when I transition from screen to screen, maybe this is a bit of overkill, but it also helps to ensure I have no leaks (see what I mean about obsessive :) )

    Likes: atilim

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    I actually call collectgarbage() when I transition from screen to screen,
    totally agree. IMHO, screen transition is also one of the best places to put collectgarbage()
  • I had a problem and fixed it with collectgarbage at the beginning on my page selector routine, but when I placed it as the end, I have a menu that slides in with the movie clip, and the garbage collector collected the movie clip so the movie clip stopped.
    So my thought is do it first rather than last.
    REAL programmers type copy con filename.exe
    ---------------------------------------
Sign In or Register to comment.