Composer : how to correctly release/optimize memory

Hi community.

I need some advice/tips to correctly release memory from scene to scene using Composer.

I’ve been reading some documentation about the subject and my understanding is that all events need to be removed, timers need to be stopped, display groups should be destroyed.

My question is when to do this? At the scene:destroy or at the scene:hide (at the event will or did?) function?

Another question is, for instance,  if i have a displayGroup named myGroup with a lot of images and call the myGroup:removeSelf() all the images are destroyed and the memory is released? 

At the end all my variables need to be equal nil to the garbage collector release the memory?

Please, can you give me some tips to implement this correctly?

Thank you so much for your time and help.

You’re close. If you put your display objects into the scene’s view group, then you don’t have to remove them. We will clear those objects when you call composer.removeScene() or the scene remove’s itself because of low memory or if you have auto remove turned on. You do not have to remove these yourself.

If you put items in a display.newGroup() of your own, it should also be self-cleaning when you make the call.

Once you’ve removed items, the garbage collector will run when it’s next scheduled to and free the memory. You are responsible for nilling  your own variables.

Rob

At this moment i have something like this:

[lua]function scene:hide( event )
print(“scene:hide”)

screenGroup = self.view
local phase = event.phase

if ( phase == “will” ) then

print(“phase=will”)

– Remove completly the scene with no recycling
composer.removeScene( “microscope”,false )

elseif ( phase == “did” ) then
– Called immediately after scene goes off screen.
print(“phase=did”)

– Stop all the event listeners
revolver:removeEventListener(“touch”, rotate.revolver);

– (0) Force the scene:destroy
composer.removeHidden()

end
end[/lua]

Then the scene:destroy is called because of the removeHidden() function.

At this point i have something like this:

[lua]

function scene:destroy( event )

    

    screenGroup = self.view

    – (1) This displayGroup is inside the screenGroup

    mainGroup:removeSelf();

    mainGroup = nil;   

    

    – (2) This displayGroup is inside the screenGroup

dynamicGrp:removeSelf();

    dynamicGrp = nil;

    

    – (3) This displayGroup has dynamicGroup and mainGroup

    screenGroup:removeSelf()

    screenGroup = nil

    – (4) Two functions 

    createDynamicGroup = nil

    createStaticGroup = nil

    – (5) Package loaded at the start 

    package.loaded[“scripts.rotate.rotate”] = nil

    rotate=nil

    

end

[/lua]

My question is: which of these instructions ( 0,1, 2,3,4,5) are correct? Are they at the right function and event?

Thank you in advance.

If you have all your display elements in the sceneView group, then the only things you have to remove in scene:destroy() are the things not in the sceneView’s group, like unloading audio, etc.

I would answer this by saying none of them are correct if you’re put everything in the scene’s view correctly.

As far as removing scenes, just remember you can’t remove the scene that’s on screen. You can remove it after you leave it and have the other scene remove the scene.

Rob

You’re close. If you put your display objects into the scene’s view group, then you don’t have to remove them. We will clear those objects when you call composer.removeScene() or the scene remove’s itself because of low memory or if you have auto remove turned on. You do not have to remove these yourself.

If you put items in a display.newGroup() of your own, it should also be self-cleaning when you make the call.

Once you’ve removed items, the garbage collector will run when it’s next scheduled to and free the memory. You are responsible for nilling  your own variables.

Rob

At this moment i have something like this:

[lua]function scene:hide( event )
print(“scene:hide”)

screenGroup = self.view
local phase = event.phase

if ( phase == “will” ) then

print(“phase=will”)

– Remove completly the scene with no recycling
composer.removeScene( “microscope”,false )

elseif ( phase == “did” ) then
– Called immediately after scene goes off screen.
print(“phase=did”)

– Stop all the event listeners
revolver:removeEventListener(“touch”, rotate.revolver);

– (0) Force the scene:destroy
composer.removeHidden()

end
end[/lua]

Then the scene:destroy is called because of the removeHidden() function.

At this point i have something like this:

[lua]

function scene:destroy( event )

    

    screenGroup = self.view

    – (1) This displayGroup is inside the screenGroup

    mainGroup:removeSelf();

    mainGroup = nil;   

    

    – (2) This displayGroup is inside the screenGroup

dynamicGrp:removeSelf();

    dynamicGrp = nil;

    

    – (3) This displayGroup has dynamicGroup and mainGroup

    screenGroup:removeSelf()

    screenGroup = nil

    – (4) Two functions 

    createDynamicGroup = nil

    createStaticGroup = nil

    – (5) Package loaded at the start 

    package.loaded[“scripts.rotate.rotate”] = nil

    rotate=nil

    

end

[/lua]

My question is: which of these instructions ( 0,1, 2,3,4,5) are correct? Are they at the right function and event?

Thank you in advance.

If you have all your display elements in the sceneView group, then the only things you have to remove in scene:destroy() are the things not in the sceneView’s group, like unloading audio, etc.

I would answer this by saying none of them are correct if you’re put everything in the scene’s view correctly.

As far as removing scenes, just remember you can’t remove the scene that’s on screen. You can remove it after you leave it and have the other scene remove the scene.

Rob