Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
how memory is released - Gideros Forum

how memory is released

I'm currently working on a snake clone and I create body parts each time it eats food. So, I create a new Bitmap for each new body part. However, when I lose, I need to erase every parts I created except the head and tail of the snake.
I use stage:removeChildAt method to remove everything from the scene. Does it frees memory of those objects or do I have to do something special with them ?

Comments

  • keszeghkeszegh Member
    be careful that there is no reference left for that bitmap and then it will be garbage collected later. not necessary to do anything. if there is enough time between restarting then you can call garbagecollect() once or twice and then you can force this to happen at that time.

  • keszeghkeszegh Member
    still, i think in such cases some pooling method is recommended where you put these elements in a table and then you can reuse them later.
  • @keszegh Thanks. So in order to completely remove, I should nil every variable that references those Bitmap?
    I'll take your pooling method in consideration. Thanks again.
  • keszeghkeszegh Member
    yes, you need to nil every reference and of course remove it from stage hierarchy.
  • olegoleg Member
    edited July 25
    local UsedMemory = function(cleanup)
            if cleanup == true then
                    collectgarbage()
            end
            local mem = collectgarbage("count")
            print( "\n Memory : " .. mem*1024 )
            return mem
    end
    -------------------------------------  
    UsedMemory(true)  --run after object deletion

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • antixantix Member
    edited July 25
    -- create the snakes head
    local head = Sprite.new()
     
    -- add 20 tail sections to the snake
    for i = 1, 20 do
      head:addChild(Sprite.new())
    end
     
    -- remove all tail sections from head
    if head:getNumChildren() > 0 then -- only proceed if there is at least 1 tail section
      for i = 1, head:getNumChildren() do
        head:removeChildAt(1) -- remove tail ection
      end
    end
     
    -- you should remove any other references to the tail sections here.
     
    -- you can opt to force garbage colection after the tail sections
    -- have been removed, or leave it to happen automatically
    collectgarbage('collect')

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.