Memory problems

I have a game with about 40 levels, and i am getting memory warnings and then my game is canceling.
I am trying to fix this problem using garbage collection…

For objects i am doing…

Object:removeSelf()  
Object = nil  

and i am canceling event listeners and timers

I am also trying to collect the garbage by

collectgarbage("collect")  

I have seen some improvement in how long you can play the game, but it still gets the same memory warning after a while.
I suspect it is my functions since alll of my functions are local… is there a way to remove them?
[import]uid: 24708 topic_id: 13438 reply_id: 313438[/import]

local functions are better than non local, typically.

When you say you have “numerous levels” can you tell us how many, even roughly? What about how much audio, physics bodies, Runtime listeners, etc?

The bigger the app the more troubleshooting it will generally take to get it running perfectly. [import]uid: 52491 topic_id: 13438 reply_id: 49446[/import]

Oh yes i meant to say that i have about 40 - 45 levels.

Theres only one audio file i use, about 4 bodies, and usually 4 runtime listeners per level.
I am currently trying to cut down the runtime listeners and the bodies.

Do you think the game is to big and i need to cut down some levels, or is there something i could do? [import]uid: 24708 topic_id: 13438 reply_id: 49475[/import]

Hmmmm, well it doesn’t sound like you’re madly overdoing it - at least not in my opinion. (This isn’t my area, although I’ve dealt with it plenty of times in my own apps.)

What kind of device are you testing on?

Are you cancelling all those Runtime listeners between scenes?

Lastly, have you found a way to bring it down from 4 Runtime listeners yet? If not, maybe you could share what they do and I could try and help you think of alternatives? :slight_smile: [import]uid: 52491 topic_id: 13438 reply_id: 49739[/import]

The device i am testing it on is 2nd generation 16GB(I need to upgrade!)… and now that you mention it, that could very well be the problem!

I am new the Corona though, and i am just wondering if Runtime listeners and event listeners are the same?

If not, I use many touch listeners threw-out my app, with not a lot of runtime listeners…

I am canceling most of them, and trying to make the code more efficient.

[import]uid: 24708 topic_id: 13438 reply_id: 49769[/import]

Runtime listeners are not the same as other listeners, no.

Here’s an example;

[lua]local function testFunction (event)
print “testing!”
end[/lua]

If you added a touch listener to your background for testFunction and looked at the terminal you’d see it print “testing!” only when you touched the background.

HOWEVER - if you instead made a Runtime listener, eg;
[lua]Runtime:addEventListener(“enterFrame”, testFunction)[/lua]

and looked at the terminal, it would be printing over and over again, super fast, until the listener was cancelled.

Clearly your device is older and could use a bit more grunt, yes - but trying to eliminate Runtime listeners when you can will help.

Did my example make sense? :slight_smile:

Peach [import]uid: 52491 topic_id: 13438 reply_id: 49872[/import]

That made perfect sense! In that case i barely have any runtime listeners… The runtimes listeners i do have consist with using accelerometer… In that case i have many event listeners (currently trying to bring it do cut some event listeners)
Thank-You for all of your help! [import]uid: 24708 topic_id: 13438 reply_id: 49901[/import]

No worries Brian,

Out of interest, do you have access to any newer devices you might test on? (Friends who have iPods/iPhones, etc?)

Peach [import]uid: 52491 topic_id: 13438 reply_id: 50145[/import]

Yeah, i know someone who has a iphone, and xcode says that i have a wrong provisional profile… I just need to get that running! [import]uid: 24708 topic_id: 13438 reply_id: 50204[/import]

Ah provisioning profiles XD

Well you’ve successfully built for your device before, obviously - so is this when you build for this person’s iPhone and try to add it via Xcode you get the error, or when you’re building for yours, or what? :slight_smile:

Peach [import]uid: 52491 topic_id: 13438 reply_id: 50387[/import]

I think the best thing you can do is strip down a version of a level.lua file you have into an example (ie take out anything specific to your game or just rename some things so it doesn’t give anything away)

And post it up here. I guarantee we will be able to help you fix it with a example file provided. It’s pretty much all guess work unless we can see what you are actually doing code wise :slight_smile: [import]uid: 6981 topic_id: 13438 reply_id: 50647[/import]

Hey guys thank you for all of your help!
I’m currently out of town for one and a half weeks and as soon as I get home ill post up some code!
Sorry for the wait! [import]uid: 24708 topic_id: 13438 reply_id: 50666[/import]

I didnt know if i was going to have time or internet access to post anything up being that i was out of town, but i have managed to have some time…

I have definitely seen a huge increase in the time available to play the game before memory problems shut it down!

I have a few types of levels but the one i think that is causing the most problems is a memory type of game(which to me feels very poorly made). In the level I’m going to show you is you have to press the colors in order according to the directions! This is just a basic version, but i dont think this is the most proficient way of doing this.

  
module(..., package.seeall)  
function new()  
 local localGroup = display.newGroup()  
  
 --hide the status bar  
 display.setStatusBar(display.HiddenStatusBar)   
  
  
 --start physics   
 local physics = require("physics");  
 physics.start();  
  
 local background = display.newImage("images/notebookpaper.png", 320, 480)  
 background.x = \_W/2  
 background.y = \_H/2  
 background.xScale = 1  
 background.yScale = 1  
  
 -- the question that stays throughout the level  
 local question = display.newText("press the colors in order", 0, 0, native.systemFont, 30);  
 question.xScale = .75  
 question.yScale = .75  
 question.x = \_W/2  
 question.y = 100  
 question.rotation = 0  
 question:setTextColor(50,50,50)  
  
 local question2 = display.newText("Blue Green Red", 0, 0, native.systemFont, 30);  
 question2.xScale2= .75  
 question2.yScale2 = .75  
 question2.x = \_W/2  
 question2.y = 140  
 question2.rotation = 0  
 question2:setTextColor(0,0,255)   
  
 -- the four circles that will be pressed  
 local redcircle = display.newCircle( \_W/2 + 40 , \_H/2 , 40)  
 redcircle:setFillColor(238,0,0)  
  
 local bluecircle = display.newCircle( 50, \_H - 50, 30)  
 bluecircle:setFillColor(0,0, 255)  
  
 local greencircle = display.newCircle ( \_W/2, \_H/2 +100, 50)  
 greencircle:setFillColor(127,255,0)  
  
 local orangecircle= display.newCircle ( \_W/4, \_H/2 + 20, 39)  
 orangecircle:setFillColor(255,165,0)  
  
 -- When the Blue button is touched...  
 function changeStage1(e)  
 if(e.phase == "began") then  
 --remove the listener for the blue circle since no longer used  
 bluecircle:removeEventListener("touch", changeStage1)  
 -- the question2 is set to no longer visible so you cant see order  
 question2.alpha = 0  
 question2.isVisible = false  
 -- green circle listener is added   
 greencircle:addEventListener("touch", changeStage2)  
 end  
 return true  
 end  
  
 bluecircle:addEventListener("touch", changeStage1)  
  
 function changeStage2(e)  
 if(e.phase == "began") then  
 -- when green is touched it removes greens listener  
 greencircle:removeEventListener("touch", changeStage2)  
 -- and adds red circle's listener  
 redcircle:addEventListener("touch", changeStage3)  
 end  
 return true  
 end  
  
 function changeStage3(e)  
 if(e.phase == "began") then  
 --when red is touched, it removes red listener  
 redcircle:removeEventListener("touch", changeStage3)  
 -- the background is needed so when the user touches anywhere else  
 -- except the correct color button it goes back to the start  
 background:removeEventListener("touch", wronganswer)  
 question:removeSelf()  
 question = nil  
 -- removing the circles  
 redcircle:removeSelf()  
 redcircle = nil  
 bluecircle:removeSelf()  
 bluecircle = nil  
 orangecircle:removeSelf()  
 orangecircle = nil  
 greencircle:removeSelf()  
 greencircle = nil  
 background:removeSelf()  
 background = nil  
 director:changeScene("level 2");  
 end  
 return true  
 end  
  
 -- function if user hits wrong answer  
 function wronganswer(e)  
 if(e.phase == "began") then  
  
 background:removeEventListener("touch", wronganswer)  
 question:removeSelf()  
 question = nil  
 question2:removeSelf()  
 question2 = nil  
 redcircle:removeSelf()  
 redcircle = nil  
 bluecircle:removeSelf()  
 bluecircle = nil  
 orangecircle:removeSelf()  
 orangecircle = nil  
 greencircle:removeSelf()  
 greencircle = nil  
 background:removeSelf()  
 background = nil  
 director:changeScene("fail");  
 end  
 end  
  
 end  
  
 background:addEventListener("touch", wronganswer)   
  
 localGroup:insert(background)  
 localGroup:insert(question)  
 localGroup:insert(question2)  
 localGroup:insert(redcircle)  
 localGroup:insert(bluecircle)  
 localGroup:insert(greencircle)  
 localGroup:insert(orangecircle)  
  
 return localGroup;  
end  

[import]uid: 24708 topic_id: 13438 reply_id: 51396[/import]

I know how to REMOVE and objec from memory using nil,

but could you show me how to REMOVE a Runtime Listener
from memory ?

Maybe a snippet of code from the great peach ? :smiley:

[import]uid: 11094 topic_id: 13438 reply_id: 52676[/import]

Runtime:removeEventListener("enterFrame", move)  

this is a snippet of code from one of my codes to remove runtime listener, but not from peach! [import]uid: 24708 topic_id: 13438 reply_id: 52677[/import]

I was’nt thinking. thanks brian0405. Sometimes the trees block the forest.

but lets says you wanted to remove ALL the eventlisteners at once. Is there a function/table that has the number and names of all Runtime (or any/all listerners) ? That way you could cycle through them and pick the ones you want.

I mean, this is LUA we are talking about. They have a table for everything ? right ?

[import]uid: 11094 topic_id: 13438 reply_id: 52688[/import]

I do not see why there couldn’t be a table to remove event Listeners. I am not sure how it would it would be implemented and made( i need to learn more in tables)… Its a great question though! i looked on alot of the forums to see if it was already made, and i came didnt come across anything! I would love to see that! [import]uid: 24708 topic_id: 13438 reply_id: 52694[/import]

Troy! Hello my friend :smiley: Nice to see you about the forum!

Sorry the code wasn’t from me :wink:

Now, first up - it’s not “LUA” :stuck_out_tongue: It is not an abbreviation. It’s Lua :wink: (Some people get really upset about that, apparently!)

Secondly - bah. Tables.

Make a function at the end of each scene that lists all your Runtime events within it/removes them when called, and use that instead.

Faster than a table and would do the exact same thing as I think you’re asking for.

MORE CAFFEINE.

Peach :wink: [import]uid: 52491 topic_id: 13438 reply_id: 52728[/import]

@brian0405, have you tried adding “local” in front of the functions like changeStage2 and changeStage3? Those functions are currently global and have references (in Lua they are called upvalues) to redCircle, blueCircle, etc. That might be the culprit. [import]uid: 26 topic_id: 13438 reply_id: 52789[/import]