Problem: Lua Memory is ever increasing when composer.removeScene is used with any newSnapshots placed into the sceneGroup.
Example: As a test case, consider scene1 and scene2. Each one creates 100 rectangles, puts them into a newSnapshot and adds that to the sceneGroup with:
sceneGroup:insert(snapshot)
The scenes pingpong between each other. With snapshots and composer.removeScene, the lua memory keeps going up and up on each new scene. But with only one of these things e.g. removeScene without snapshots or snapshots but no removeScene then the memory returns to a baseline on each new scene (with a garbage collection).
The following familiar code is from scene1 as it removes scene2. The code is the same on scene2 except it removes scene1.
function scene:create( event ) local sceneGroup = self.view local width, height = display.contentWidth, display.contentHeight local halfW, halfH = width\*0.5, height\*0.5 local bg = display.newRect( halfW, halfH, width, height ) bg:setFillColor( 0.2 ) sceneGroup:insert(bg) -- Add to Snapshot local snapshot = display.newSnapshot( width, height ) math.randomseed( os.time() ) -- Add rect to the screen for i=1,100 do local item = display.newRect( halfW, halfH, halfW, halfH ) item:setFillColor( 1,1,1,0.1 ) -- move to random position in a 200x200 region in the middle of the screen local randX, randY = math.random( -halfW, halfW ), math.random( -halfH, halfH ) item:translate( randX, randY ) -- insert item into snapshot snapshot.group:insert( item ) end sceneGroup:insert(snapshot) snapshot:translate( halfW, halfH ) -- Center snapshot snapshot:invalidate() -- Invalidate snapshot snapshot.alpha = 0.5 -- Apply to flattened image end function scene:show( event ) local sceneGroup = self.view local phase = event.phase local currScene = composer.getSceneName( "current" ) if phase == "will" then elseif phase == "did" then if currScene == "scene1" then if nil~= composer.getScene("scene2") then composer.removeScene("scene2", true) end -- Destroy for previous Scene gets called print("------- call remove Scene\_2 -------") else composer.removeScene("scene1", true) -- Destroy for previous Scene gets called print("------- call remove Scene\_1 -------") end --composer.removeHidden() -- Goto Next scene local goto = timer.performWithDelay(500, function() composer.gotoScene( "scene2", "fade", 250 ) end) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then timer.performWithDelay(1, function() collectgarbage("collect") end) elseif phase == "did" then end end