Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
box2d object with spring - Page 3 — Gideros Forum

box2d object with spring

13»

Comments

  • Hi...
    Sorry for asking silly questions again and again..

    I went through the links you provides above, but I didn't understand how to free the memory each time.. I found following code
    local function gc()
    	collectgarbage()
    	collectgarbage()
    	collectgarbage()
    	collectgarbage()
    end
     
    gc()
    local mem1 = collectgarbage("count")
     
    local t = {}		-- create an empty table
    for i=1,10000 do 	-- put another 10000 empty tables in it
    	t[i] = {}
    end
     
    gc()
    local mem2 = collectgarbage("count")
     
    for i=1,10000 do	-- empty the table (now this table has no fields)
    	t[i] = nil
    end
     
    gc()
    local mem3 = collectgarbage("count")
     
    t = nil				-- set table as nil
     
    gc()
    local mem4 = collectgarbage("count")
     
    print(mem1, mem2, mem3, mem4)
    I just understood that collectgarbage() is use to collect the memory leak...
    But Did't understand how and where to use these codes in the project...

    I simply added
            collectgarbage()
    	collectgarbage()
    	collectgarbage()
    	collectgarbage()
            print(collectgarbage("count"))
    in all
    function EndScene:onExitBegin()
     
    end
    of each lua files...
    Each time i replay the games the count increases...

    No idea what to do...
  • @Sush19
    Some tips;
    -In scenemanager when changing scenes remove not used listeners.
    -Try not to use global variables, as far as i know global variables eats more memory, change them to local which you can of course.
    -Do not load all your assets in the very beginning if you will not use them. Whenever your job is finished with an asset if it will not be repeatedly used, null it so that collectgarbage can free space.

    Your problem "increase in replays" can be caused by in every replay new object will be created without disposing the old one.

  • Hello @talis,
    Thank you for your tips...
    I've only 3 Scenes
    1st Scene has only one background image with a play button image and a event listener for the button, and also with background sound...

    2nd Scene is the game Scene where I'm loading entire images, fonts, sounds and all with event listeners...

    3rd Scene is similar to 1st Scene, with different background, sounds, total score, bonus and replay button...

    On game over I'me changing the Scene, i.e from 2nd Scene to 3rd Scene
    And on replay I'm just changing the Scene, i.e from 3rd Scene to 2nd Scene...

    How do I dispose the old sprites and all...?
    Is there any method to remove all the sprites and make it nil from the Scene, or we have to manually do it for each sprites... :)
  • if you add all to the scene then it should be removed automatically, when changed to other scene (it won't happen instantly and could take some time)

    But in some cases, you may leave global objects (forgetting to add local before, etc, which can leak memory.)

    You can try out this classes by @bowerandy to print out all the current objects:
    http://giderosmobile.com/forum/discussion/comment/20247#Comment_20247

    And here are the instructions on how to set this up:
    http://giderosmobile.com/forum/discussion/comment/20257#Comment_20257
  • @Sush19 I was thinking about your case and most probably you do not remove onEnterFrame event when exiting scene.

    Put something like that in your game scene (change class and method names to what you use):
    GameScene = Core.class(Sprite)
     
    function GameScene:init()
        self:addEventListener("exitBegin", self.onExitBegin, self)
    end
     
    function GameScene:onExitBegin()
    	self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
Sign In or Register to comment.