Memory usage does not go down. Help with Memory Leak

I am trying to track down a memory leak in my app, and I am having quite a hard time.

Below is code for 2 scenes which, when you switch between them, add to the memory usage slightly. I keep on doing this and the memory just NEVER goes down! What am I doing wrong here?

PS: Both basically only has a clickable image that alternates going from one to the other.

menu.lua:
[lua]local storyboard = require(“storyboard”);
local scene = storyboard.newScene();
storyboard.state = {}
function scene:createScene(e)
local view = self.view
local function onAdTap( self, event )
if (event.phase == “began”) then
storyboard.gotoScene( “about” )
end
end

local ad = display.newImageRect(“adsample.png”, _W, 64)
ad:setReferencePoint(display.CenterReferencePoint);
ad.x = _W/2 ad.y = _H/2
ad.touch = onAdTap – the function we created previously
ad:addEventListener( “touch” )
view:insert(ad);
end

function scene:exitScene(e) storyboard.removeScene(“menu”) end
function scene:enterScene(e)
local monitorMem = function()

collectgarbage()
print( "MemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )
end

Runtime:addEventListener( “enterFrame”, monitorMem )

local prior_scene = storyboard.getPrevious()
if (prior_scene) then
storyboard.purgeScene( prior_scene )
end
end
scene:addEventListener(“enterScene”, scene);
scene:addEventListener(“createScene”, scene);
scene:addEventListener(“exitScene”, scene);

return scene;[/lua]

about.lua:
[lua]local storyboard = require(“storyboard”);
local scene = storyboard.newScene();

function scene:createScene(e)
local view = self.view
local function onAdTap( self, event )
if (event.phase == “began”) then
storyboard.gotoScene( “menu” )
end
end
local ad = display.newImageRect(“adsample.png”, _W, 64)
ad:setReferencePoint(display.CenterReferencePoint);
ad.x = _W/2 ad.y = _H/2
ad.touch = onAdTap – the function we created previously
ad:addEventListener( “touch” )
view:insert(ad);
end

function scene:exitScene(e) storyboard.removeScene(“about”) end
function scene:destroyScene(event) end
function scene:enterScene(e)
local prior_scene = storyboard.getPrevious()
if (prior_scene) then
storyboard.purgeScene( prior_scene )
end
end

scene:addEventListener(“enterScene”, scene);
scene:addEventListener(“destroyScene”, scene )
scene:addEventListener(“createScene”, scene);
scene:addEventListener(“exitScene”, scene);
return scene;[/lua]

Please help.

Thank you so much.

John

PS: I have read these:

And have tried to see if they help but to no avail. :frowning:

I even set:
[lua]storyboard.purgeOnSceneChange = true[/lua] [import]uid: 186198 topic_id: 32700 reply_id: 332700[/import]

you are not removing the png.
try something like this:

function scene:createScene(e)  
 local view = self.view  
 local ad = display.newImageRect("adsample.png", \_W, 64)  
  
 local function onAdTap( self, event )  
 if (event.phase == "began") then  
 ad:removeSelf() -- remove display object  
 ad = nil -- set object to nil  
 storyboard.gotoScene( "menu" )  
 end  
 end  
  
 ad:setReferencePoint(display.CenterReferencePoint);  
 ad.x = \_W/2 ad.y = \_H/2  
 ad.touch = onAdTap -- the function we created previously  
 ad:addEventListener( "touch" )  
 view:insert(ad);  
end  

-finefin [import]uid: 70635 topic_id: 32700 reply_id: 129984[/import]

(reply for your #1 post)

Remove ad event listener in the scene.exitScene

I don’t think you should remove the ad variable manually, as long as you insert that into scene.view (group), it will be done automatically when you leave the scene.
Transition, timer, listener and sounds that should be remove manually.

and if you want to remove menu.lua, your storyboard.removeScene must be place at about.lua and using storyboard.getPrevious to know the previous scene.

something like this:

 local previousScene = storyboard.getPrevious()  
 storyboard.removeScene( previousScene )  

write this in the about.lua to remove menu.lua
Also read this http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/ [import]uid: 31508 topic_id: 32700 reply_id: 129991[/import]

You don’t need to manually remove it. As long as you add your display objects to the scene’s view/group like you are, the image will get removed when the scene is purged/removed.

I think the reason you’re not seeing a change is that both modules have to be active to transition one on and one off and you’re printing your memory before you purge, so both scenes are always still in memory when you print your stats. Do your print after you do the removeScene/purgeScene call and see if you are happier. [import]uid: 19626 topic_id: 32700 reply_id: 129993[/import]

you are not removing the png.
try something like this:

function scene:createScene(e)  
 local view = self.view  
 local ad = display.newImageRect("adsample.png", \_W, 64)  
  
 local function onAdTap( self, event )  
 if (event.phase == "began") then  
 ad:removeSelf() -- remove display object  
 ad = nil -- set object to nil  
 storyboard.gotoScene( "menu" )  
 end  
 end  
  
 ad:setReferencePoint(display.CenterReferencePoint);  
 ad.x = \_W/2 ad.y = \_H/2  
 ad.touch = onAdTap -- the function we created previously  
 ad:addEventListener( "touch" )  
 view:insert(ad);  
end  

-finefin [import]uid: 70635 topic_id: 32700 reply_id: 129984[/import]

Thanks for the reply, I tried your suggestion to print out the memory usage when entering individual scenes.

I’m like whoa! I am wondering why Game Scene EnterScene is called 4 times though.

This just means I have to seriously clean up my code. I can paste 1 scene here if needed though, so once I get what’s wrong with that scene, I can apply it to the rest.

John [import]uid: 186198 topic_id: 32700 reply_id: 129996[/import]

Thanks for the suggestion. I have that under each of my scenes’s enterScene now,not much of a difference.

I wonder what am I doing wrong. :frowning:

John

Here’s the menu scene in case anyone is interested:

[lua]local storyboard = require(“storyboard”);
local json = require (“json”);
local scene = storyboard.newScene();
storyboard.state = {}

function scene:createScene(e)
local view = self.view
local menuBG = display.newImageRect(“images/menu/menuBackground.png”, _W, _H)
local titleText = display.newText(“Title”, 0, 0, font, 50);
local versionText = display.newText(“v” … app_version, 0, 0, font, 12);
local buildDateText = display.newText(build_date, 0, 0, font, 12);
local menuButton = {};

menuBG:setReferencePoint(display.TopLeftReferencePoint);
menuBG.x = 0 menuBG.y = 0
view:insert(menuBG);

titleText:setTextColor(0,0,0);
titleText:setReferencePoint( display.CenterReferencePoint )
titleText.x = _W / 2
titleText.y = _H /2 - _H/4
view:insert(titleText);

versionText:setTextColor(0,0,0);
versionText:setReferencePoint( display.CenterReferencePoint )
versionText.x = _W / 2
versionText.y = _H - 50
view:insert(versionText);

buildDateText:setTextColor(0,0,0);
buildDateText:setReferencePoint( display.CenterReferencePoint )
buildDateText.x = _W / 2
buildDateText.y = _H - 35
view:insert(buildDateText);

menuButton.new = function(params)
local btn = display.newGroup();

local offIMG = params and params.off or “images/menu/menuButton_unpressed.png”;
local onIMG = params and params.on or “images/menu/menuButton_pressed.png”;

local off = display.newImageRect(offIMG, params.xSize, params.ySize);
local on = display.newImageRect(onIMG, params.xSize, params.ySize);

local font = “HelveticaNeue” or native.systemFont;
local label = display.newText(params.label,0,0,font,20);
label:setTextColor(0,0,0);
label:setReferencePoint( display.CenterReferencePoint )
label.x = 0;
label.y = 0;

on.alpha = 0;
btn:insert(off);
btn:insert(on);
btn:insert(label);

btn.x = params and params.x or 0;
btn.y = params and params.y or 0;

function btn:touch(e)
if(e.phase == “began”) then
on.alpha = 1;–make the on-state visible
display.getCurrentStage():setFocus(self);
self.hasFocus = true;
elseif(self.hasFocus) then
if(e.phase == “ended”) then
on.alpha = 0;–make the on-state invisible
if (params.id == “classic”) then
storyboard.state.mode = “classic”
storyboard.state.dimensions = 5
storyboard.gotoScene( “levelSelect”, “crossFade”, 100 )
elseif (params.id == “timetrial”) then
storyboard.state.mode = “timetrial”
storyboard.state.solved = 0
storyboard.state.time = 0
storyboard.gotoScene( “dimension_select”, “crossFade”, 100 )
end
display.getCurrentStage():setFocus(nil);
self.hasFocus = false;
end
end
end

btn:addEventListener(“touch”,btn);

return btn;
end --end Button class declaration

local classicParams = {
id = “classic”, label = “Classic”,
xSize = 154, ySize = 40,
x = _W / 3, y = _H / 2
}
local classicButton = menuButton.new(classicParams);
view:insert(classicButton);

local timeTrialParams = {
id = “timetrial”, label = “Time Attack”,
xSize = 154, ySize = 40,
x = _W / 3, y = (_H / 2) + 50
}
local timeTrialButton = menuButton.new(timeTrialParams);
view:insert(timeTrialButton);
end

function scene:exitScene(e) storyboard.removeScene(“menu”) end

function scene:enterScene(e)
local prior_scene = storyboard.getPrevious()
if (prior_scene) then
storyboard.purgeScene( prior_scene )
end

local monitorMem = function()
print(“Menu Screen”)
collectgarbage()
print( "MemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )
end
monitorMem()
end

scene:addEventListener(“enterScene”, scene);
scene:addEventListener(“createScene”, scene);
scene:addEventListener(“exitScene”, scene);

return scene;[/lua] [import]uid: 186198 topic_id: 32700 reply_id: 129997[/import]

(reply for your #1 post)

Remove ad event listener in the scene.exitScene

I don’t think you should remove the ad variable manually, as long as you insert that into scene.view (group), it will be done automatically when you leave the scene.
Transition, timer, listener and sounds that should be remove manually.

and if you want to remove menu.lua, your storyboard.removeScene must be place at about.lua and using storyboard.getPrevious to know the previous scene.

something like this:

 local previousScene = storyboard.getPrevious()  
 storyboard.removeScene( previousScene )  

write this in the about.lua to remove menu.lua
Also read this http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/ [import]uid: 31508 topic_id: 32700 reply_id: 129991[/import]

Oh I see. Now if we try to scale it larger, should I do that to every image I add to a scene and then remove them all when switching scenes?

John [import]uid: 186198 topic_id: 32700 reply_id: 129987[/import]

You don’t need to manually remove it. As long as you add your display objects to the scene’s view/group like you are, the image will get removed when the scene is purged/removed.

I think the reason you’re not seeing a change is that both modules have to be active to transition one on and one off and you’re printing your memory before you purge, so both scenes are always still in memory when you print your stats. Do your print after you do the removeScene/purgeScene call and see if you are happier. [import]uid: 19626 topic_id: 32700 reply_id: 129993[/import]

Thanks for the reply, I tried your suggestion to print out the memory usage when entering individual scenes.

I’m like whoa! I am wondering why Game Scene EnterScene is called 4 times though.

This just means I have to seriously clean up my code. I can paste 1 scene here if needed though, so once I get what’s wrong with that scene, I can apply it to the rest.

John [import]uid: 186198 topic_id: 32700 reply_id: 129996[/import]

Thanks for the suggestion. I have that under each of my scenes’s enterScene now,not much of a difference.

I wonder what am I doing wrong. :frowning:

John

Here’s the menu scene in case anyone is interested:

[lua]local storyboard = require(“storyboard”);
local json = require (“json”);
local scene = storyboard.newScene();
storyboard.state = {}

function scene:createScene(e)
local view = self.view
local menuBG = display.newImageRect(“images/menu/menuBackground.png”, _W, _H)
local titleText = display.newText(“Title”, 0, 0, font, 50);
local versionText = display.newText(“v” … app_version, 0, 0, font, 12);
local buildDateText = display.newText(build_date, 0, 0, font, 12);
local menuButton = {};

menuBG:setReferencePoint(display.TopLeftReferencePoint);
menuBG.x = 0 menuBG.y = 0
view:insert(menuBG);

titleText:setTextColor(0,0,0);
titleText:setReferencePoint( display.CenterReferencePoint )
titleText.x = _W / 2
titleText.y = _H /2 - _H/4
view:insert(titleText);

versionText:setTextColor(0,0,0);
versionText:setReferencePoint( display.CenterReferencePoint )
versionText.x = _W / 2
versionText.y = _H - 50
view:insert(versionText);

buildDateText:setTextColor(0,0,0);
buildDateText:setReferencePoint( display.CenterReferencePoint )
buildDateText.x = _W / 2
buildDateText.y = _H - 35
view:insert(buildDateText);

menuButton.new = function(params)
local btn = display.newGroup();

local offIMG = params and params.off or “images/menu/menuButton_unpressed.png”;
local onIMG = params and params.on or “images/menu/menuButton_pressed.png”;

local off = display.newImageRect(offIMG, params.xSize, params.ySize);
local on = display.newImageRect(onIMG, params.xSize, params.ySize);

local font = “HelveticaNeue” or native.systemFont;
local label = display.newText(params.label,0,0,font,20);
label:setTextColor(0,0,0);
label:setReferencePoint( display.CenterReferencePoint )
label.x = 0;
label.y = 0;

on.alpha = 0;
btn:insert(off);
btn:insert(on);
btn:insert(label);

btn.x = params and params.x or 0;
btn.y = params and params.y or 0;

function btn:touch(e)
if(e.phase == “began”) then
on.alpha = 1;–make the on-state visible
display.getCurrentStage():setFocus(self);
self.hasFocus = true;
elseif(self.hasFocus) then
if(e.phase == “ended”) then
on.alpha = 0;–make the on-state invisible
if (params.id == “classic”) then
storyboard.state.mode = “classic”
storyboard.state.dimensions = 5
storyboard.gotoScene( “levelSelect”, “crossFade”, 100 )
elseif (params.id == “timetrial”) then
storyboard.state.mode = “timetrial”
storyboard.state.solved = 0
storyboard.state.time = 0
storyboard.gotoScene( “dimension_select”, “crossFade”, 100 )
end
display.getCurrentStage():setFocus(nil);
self.hasFocus = false;
end
end
end

btn:addEventListener(“touch”,btn);

return btn;
end --end Button class declaration

local classicParams = {
id = “classic”, label = “Classic”,
xSize = 154, ySize = 40,
x = _W / 3, y = _H / 2
}
local classicButton = menuButton.new(classicParams);
view:insert(classicButton);

local timeTrialParams = {
id = “timetrial”, label = “Time Attack”,
xSize = 154, ySize = 40,
x = _W / 3, y = (_H / 2) + 50
}
local timeTrialButton = menuButton.new(timeTrialParams);
view:insert(timeTrialButton);
end

function scene:exitScene(e) storyboard.removeScene(“menu”) end

function scene:enterScene(e)
local prior_scene = storyboard.getPrevious()
if (prior_scene) then
storyboard.purgeScene( prior_scene )
end

local monitorMem = function()
print(“Menu Screen”)
collectgarbage()
print( "MemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )
end
monitorMem()
end

scene:addEventListener(“enterScene”, scene);
scene:addEventListener(“createScene”, scene);
scene:addEventListener(“exitScene”, scene);

return scene;[/lua] [import]uid: 186198 topic_id: 32700 reply_id: 129997[/import]

Oh I see. Now if we try to scale it larger, should I do that to every image I add to a scene and then remove them all when switching scenes?

John [import]uid: 186198 topic_id: 32700 reply_id: 129987[/import]

Anyone? Please help?:frowning: [import]uid: 186198 topic_id: 32700 reply_id: 130083[/import]

Your problem is memory leaks right?
Why not try my suggestion to remove the event listener when you leave the scene?
(line 86)

And line 108 , storyboard.removeScene doesn’t work on active scene.
[import]uid: 31508 topic_id: 32700 reply_id: 130085[/import]

@bpran:

Your problem is memory leaks right?
Why not try my suggestion to remove the event listener when you leave the scene?
(line 86)

And line 108 , storyboard.removeScene doesn’t work on active scene.

[lua]local prior_scene = storyboard.getPrevious()
storyboard.purgeScene( prior_scene )[/lua]

Thanks for the reply.

John [import]uid: 186198 topic_id: 32700 reply_id: 130087[/import]

you can call storyboard.removeScene(“nextscenename”)

before calling storyboard.gotoScene(“nextscenename”)

[import]uid: 19626 topic_id: 32700 reply_id: 130089[/import]

Anyone? Please help?:frowning: [import]uid: 186198 topic_id: 32700 reply_id: 130083[/import]

@bpran:

Well, I was able to remove the event listeners. That shaved off some memory! Still looking for more stuff to shave off. Thanks for the suggestion!

@robmiracle:

So I remove it before calling it? I’ll try that out. Thank you for the suggestion!

John [import]uid: 186198 topic_id: 32700 reply_id: 130091[/import]

Your problem is memory leaks right?
Why not try my suggestion to remove the event listener when you leave the scene?
(line 86)

And line 108 , storyboard.removeScene doesn’t work on active scene.
[import]uid: 31508 topic_id: 32700 reply_id: 130085[/import]