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...

1
2
Object:removeSelf()
Object = nil

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.

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?

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? :)

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.

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

Here's an example;

1
2
3
local function testFunction (event)
print "testing!"
end

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!

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

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!

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? :)

Peach

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 :)

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!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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

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 ? :D

1
Runtime:removeEventListener("enterFrame", move)

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 ?

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!

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

Sorry the code wasn't from me ;)

Now, first up - it's not "LUA" :P It is not an abbreviation. It's Lua ;) (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 ;)

@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.

views:2473 update:2011/10/4 8:06:35
corona forums © 2003-2011