CleanUp issues

Hi everyone, now i finished one of my apps, and i am working now on cleanUp.
I am using director with popUps etc, and i never worked with clean ups and really dont know how to use it etc, i was searching and saw some posts on corona forums and the topic on corona website, some like, memory leak, and understand a little about it, but when i tried on my code, it give me errors and i think that i used it badly, i use a library that his name is cleanGroup.lua, it was talked by jonathan beebe on his site, and i tried to use it, but dont saw any results, i really dont know how i can use it on my code, can anyone give me some explication about how i can remove my local group and where i place this clean up codes, on top or on the end of code, using functions or no, some steps with a little good explications if possible, this part is really important to learn, and is the last part to finish my app, i accept all helps, tutorials, codes, etc, only want to learn :slight_smile:

Thanks [import]uid: 26056 topic_id: 22128 reply_id: 322128[/import]

If you insert every visual object into the localGroup when using director they should be removed when you change scenes - no need for additional classes.

Try that, remove the cleanGroup.lua file and references to it, then if you have any errors post them and will see how we can help :slight_smile: [import]uid: 52491 topic_id: 22128 reply_id: 88018[/import]

i am using this:

local PlayTouch = function( event )  
 LocalGroup:remove()  
 director:changeScene("game")   
 end   
  
 local PlayButton = widget.newButton{  
  
 onRelease = PlayTouch  
 }  

but the mem usage is increasing every time that i change between this menu.lua and game.lua and i am removing from both of files.

i use some physics/transitions/timers/runtimes/collisions. really need to understand how i can clean all this transitions/timers etc and where i put the function to clean all ? when i touch on a button to change to other screen? really need help, it is the most important thing to my app runs great.
Thanks [import]uid: 26056 topic_id: 22128 reply_id: 88183[/import]

The localGroup should automatically be removed on scene change.

LocalGroup is not the same as localGroup - why is that in there? Did you make a group named that?

Are you running the terminal? If so, are there errors?

Runtime listeners and timers you would remove immediately before scene change in that function. [import]uid: 52491 topic_id: 22128 reply_id: 88274[/import]

Yeh i use with that name, LocalGroup, timers and runtimes are removed when i remove the local group? i dont have errors on terminal [import]uid: 26056 topic_id: 22128 reply_id: 88318[/import]

How long is your level file? If you share some more code maybe I can help you spot the issue. [import]uid: 52491 topic_id: 22128 reply_id: 88409[/import]

Hi peach it is a little long, but per example, for my main menu, i only use widget buttons and backgrounds images, and when change to my slider page and then back again to my main menu, it will increasing the mem usage each time that i change the screen and i am removing the group when i change to between the screens. i put LocalGroup:remove() on my function that i use to my widget button, the code is on the top

and for example i use two types of transitions, like that:

transition.to(Star, {time=1000, x = \_W\*0.3, y = \_H\*0.2, xScale = 1, yScale = 1, alpha = 1})  
  
and transition.to(hello, {time=100,onComplete = Screen })  

i use a lot of this type of transitions, i really want to clean all my transitions, all the time that i change for other screen. and the last code that i need to clean are the runtimes

local function myRuntime (event)  
  
 -- Code   
  
 Runtime:addEventListener("enterFrame",myRuntime )  

Need to clean:

  • Images
  • Runtimes
  • Transitions

Thanks for all the helps, if need some aditional information tell me what you need :slight_smile:
[import]uid: 26056 topic_id: 22128 reply_id: 88423[/import]

Transitions wont be an issue most likely.

What you need to do in the function where you change scenes is do;

Runtime:removeEventListener() - the same way you added them but using “remove” instead of “add”.

For timers you must do that too;
timer.cancel()

Images in director’s localGroup will automatically be removed. [import]uid: 52491 topic_id: 22128 reply_id: 88575[/import]

I am tring now again to resolve it, i am on my main menu, i only have widget buttons with the respective functions of each button, ice library, one some images and nothing more, and i using my librarys like director, ice on my main.lua and i am using it with this code…

require(“director”)
require(“ice”)
it is not wrong?

or i should use

local director = require (“director”)
local ice = require (“ice”)

what i should use? and for the widget library i am using

local widget = require (“widget”)

i need to use this code each file that i use widget buttons, i cant put this code only one time on my main.lua and then run widget button on my main menu, if i want to run widget buttons on main menu i need to put this code on the main menu too.

maybe it can be increasing my memory usage each time that i switch between two different screens? i am removing my localgroup and i tried to put localgroup = ni, i dont know if it can clean some memory or no, but i didnt see any effect using that.

and i tried on a different project, only with three files.
main.lua
mainmenu.lua
game.lua

and put only a widget button on the mainmenu.lua and on game.lua, and then will changing the screen by pressing on the button, and it will increasing my memory usage each time that i change the screen. the code is:

main.lua

-- Remove head bar  
display.setStatusBar( display.HiddenStatusBar)  
  
-- Screen   
\_W = display.contentWidth  
\_H = display.contentHeight  
  
-- Load librarys  
local director = require("director")  
  
  
-- Create a main group  
local MainGroup = display.newGroup();  
  
  
-- Main function  
  
local function Main()  
local function monitorMem(event)  
 collectgarbage("collect")   
 print( "\nMemUsage: " .. (collectgarbage("count")/1000) .. " MB")  
 print("Texture Usage " .. system.getInfo( "textureMemoryUsed" ) / 1000000)   
 return true  
 end  
 Runtime:addEventListener("enterFrame", monitorMem)  
  
  
 MainGroup:insert(director.directorView)  
 director:changeScene("mainmenu")  
 return true  
  
end  
  
Main()  
  

mainmenu.lua

  
module(..., package.seeall)  
  
function new()  
local LocalGroup = display.newGroup();  
local widget = require "widget"  
  
 local PlayTouch = function( event )  
 LocalGroup:remove()  
 LocalGroup = nil  
 director:changeScene("game")  
  
  
 end   
  
 local PlayButton = widget.newButton{  
 default = "play\_button.png",  
 over = "play\_button\_clicked.png",  
 onRelease = PlayTouch  
 }  
  
 PlayButton.x = math.round(\_W\*0.5)  
 PlayButton.y = math.round(\_H\* 0.79)  
 PlayButton.isVisible = true;  
 PlayButton.view:toFront();  
 LocalGroup:insert(PlayButton.view);  
  
return LocalGroup;  
  
end  
   

game.lua

  
module(..., package.seeall)  
  
function new()  
local LocalGroup = display.newGroup();  
  
local widget = require "widget"  
  
 local PlayTouch = function( event )  
 LocalGroup:remove()  
 LocalGroup = nil  
 director:changeScene("mainmenu")  
  
  
 end   
  
 local PlayButton = widget.newButton{  
 default = "play\_button.png",  
 over = "play\_button\_clicked.png",  
 onRelease = PlayTouch  
 }  
  
 PlayButton.x = math.round(\_W\*0.5)  
 PlayButton.y = math.round(\_H\* 0.5)  
 PlayButton.isVisible = true;  
 PlayButton.view:toFront();  
 LocalGroup:insert(PlayButton.view);  
  
return LocalGroup;  
  
end  
   
  

[import]uid: 26056 topic_id: 22128 reply_id: 88699[/import]

Put in “local” before requiring, as you said :slight_smile:

Memory usage will vary a bit, if you flick back and forth between two screens is it continuing to go up or is it stable? [import]uid: 52491 topic_id: 22128 reply_id: 88795[/import]

it is increasing when i am changeing between two screens the values is some like that:

1º screen
memUsage:0.18165…
texture:0.065536

now go to the screen 2

2º screen
memUsage:0.18321…
texture:0.065536

now go back to the screen 1

1º screen
memUsage:0.183315…
texture:0.065536

go again to 2 screen

2º screen
memUsage:0.18342…
texture:0.065536

and it will increasing every time, here is a very litle code, but on my game with much more code, the memory usage still increasing every time that i change the scenes. i really want to fix this problem, because i can publish my app increasing every time the memUsage :confused:

Thanks [import]uid: 26056 topic_id: 22128 reply_id: 88829[/import]

This actually looks fine - the memory usage and texture usage are tiny and the increase is not occurring every time, in fact it drops at one point there too.

I would not be concerned about this at all. [import]uid: 52491 topic_id: 22128 reply_id: 88980[/import]

Test this example and see the memUsage values plz

main.lua

-- Remove head bar  
display.setStatusBar( display.HiddenStatusBar)  
  
-- Screen   
\_W = display.contentWidth  
\_H = display.contentHeight  
  
-- Load librarys  
local director = require("director")  
  
  
-- Create a main group  
local MainGroup = display.newGroup();  
  
  
-- Main function  
  
local function Main()  
local function monitorMem(event)  
 collectgarbage("collect")   
 print( "\nMemUsage: " .. (collectgarbage("count")/1000) .. " MB")  
 print("Texture Usage " .. system.getInfo( "textureMemoryUsed" ) / 1000000)   
 return true  
 end  
 Runtime:addEventListener("enterFrame", monitorMem)  
  
  
 MainGroup:insert(director.directorView)  
 director:changeScene("exp")  
 return true  
  
end  
  
Main()  
  

exp.lua

module(..., package.seeall)  
  
function new()  
  
 local LocalGroup = display.newGroup();  
 local function changeScene( event )  
 director:openPopUp("exp2")  
 return true   
 end  
  
 local myRectangle = display.newRect(0, 0, 150, 50)  
 myRectangle:setFillColor(140, 140, 140)  
 myRectangle.x = math.round(\_W\*0.5)  
 myRectangle.y = math.round(\_H\* 0.79)  
 LocalGroup:insert(myRectangle)  
  
 myRectangle:addEventListener( "touch", changeScene )  
  
return LocalGroup;  
  
end  
   

exp2.lua

module(..., package.seeall)  
  
function new()  
  
local var = 0  
local LocalGroup = display.newGroup();  
  
 local function changeScene( event )  
 var = 1  
 director:closePopUp()  
 return true   
 end  
  
 local myRectangle = display.newRect(0, 0, 150, 50)  
 myRectangle:setFillColor(140, 140, 140)  
 myRectangle.x = math.round(\_W\*0.5)  
 myRectangle.y = math.round(\_H\* 0.5)  
 LocalGroup:insert(myRectangle)  
  
 myRectangle:addEventListener( "touch", changeScene )  
  
 local StarsPhysics = function( event )  
 if var == 1 then  
 var = nil   
 LocalGroup:removeSelf()  
 LocalGroup = nil  
 Runtime:removeEventListener( "enterFrame", StarsPhysics )  
 StarsPhysics = nil  
 end  
  
 end  
  
 Runtime:addEventListener( "enterFrame", StarsPhysics )  
  
 return LocalGroup;  
  
end  
   

last file (exp2) have an error on the runtime, try to help if you can.
and fix the problem with the memory, it will be very important for me

Thanks peach [import]uid: 26056 topic_id: 22128 reply_id: 89156[/import]

You are trying to remove LocalGroup manually but you don’t need to do that, director does it for you - comment out the two lines where you remove and nil it and it will run fine.

RE memory, I can’t debug the code - only Premium Support can do that - but I wouldn’t worry about it, it’s not consistently increasing. [import]uid: 52491 topic_id: 22128 reply_id: 89229[/import]