No shame in that, great piece of code. Thanks!
If you are testing on device hover a textbox displaying memory use.
Continuously create each object and remove them to see if the memory does not get out of hand. Maybe hover a button for creating the object and another for deleting it. Maybe simplify your code to help find the problem areas. Sometimes you can expect some memory creep as the garbage collector takes time. But it should not be as bad as you are describing. It’s usually a good idea to track how memory is behaving as you build your app before it turns into a massive junkyard with cobwebs and rats.
On face value I would expect for such slowdown to occur you are not removing all the images? possibly levels are running in background underneath your newest level. You could always give each level object it’s own name (from the place that creates them) i.e
local count = 0
newLevel = Levels.new() – ok this just represents the place you’re creating levels.
newLevel.name = “level”…count.
count = count + 1
then inside some of the loops or functions that you suspect might be running (on the level object itself) get the object to print it’s own name: print(self.name). If you ever see two different levels printing at the same time you will know something fishy is up.
I might build a basic debgugger soon where you can at least print things out across the network to an app on your computer - Corona is a bit of nightmare for testing on an actual phone
I am using storyboard in my game and I have been using the storyboard.printMemUsage() to display memory usage statistics and I have observed that the texture memory usage remains the same while the system memory usage increases by about about 0.02 mbs with every level start/restart. Now I am guessing that the texture memory relates to memory taken up by the display objects, but I don’t know what all uses up the system memory…
In code all objects have been inserted into a display group which inturn has been inserted into the ‘createScene - screenGroup’. I have made sure of that. Now this is good enough to ensure that everything will be cleaned up when I change and remove scene. Right?
This is how I am creating levels. I use storyboard. I have a mainGameInterface.lua which creates the bike, the background and everything that is common in all stages. Then I have like level1.lua, level2.lua, etc. created for each level where I create the path, by arranging pngs in a specific pattern. I keep track of which level player is in using GGdata. So aacordingly I require the level2.lua(say) file in my mainGameInterface.lua
See primoz in my post above I have also mentioned how I am creating and removing levels.
Yes i see
I can’t realy tell off the top of my head what is causing your issue this will take some debuging. Maybe you are keeping some references to some objects outside the code that don’t get removed because of it. Maybe you have circular references somewhere (I was never sure whether garbage collector picks this up or not so I always remove them when done with an object). Maybe you left an enterFrame listener or a transition running that is holding on to an object. It could be one of any number of things.Without debuging your code I don’t realy know how we could help. As you or someone else mentioned, try systematicaly removing things to see when the problem goes away. But since it is commonly a problem you could post your scene create, enter, exit, destroy function and specify whether and when you remove scenes if you remove them at all, etc.
What is the meaning of circular references?
local table1 = {} local table2 = {} table1.ref2 = table2 table2.ref1 = table1
This can be any number of levels down also like table1.ref2.ref3.ref4 == table1
I’ll be glad if someone comments on this whether garbage collector would dispose of this or not when I set table1 and table2 to nil.
Here’s the file if anybody wants to have a look.
http://drive.mobieos.com/user\_uploads/mobieos\_anirbanm7856/motox\_corona\_2013\_2076.rar
mainGameInterface.lua is the file which is called for every level. All level.lua files(eg. level1, leve2, level3…) are required inside of this. I goto the scene “loading”(loading.lua) whenever I need to restart the level or load next level, and remove scene “mainGameInterface”
There is a lot of things going on there. It might not be possible to find out what is wrong without fully debugging your app. This would take some time. I just took a quick look and things that I would change of the top is:
-
in your mainGameInterface you are adding enterFrame listeners as soon as the module is crated and the scene is not created yet. Granted it will probably be created before the enterFrame listener is called but still I would move that to enterScene or at least at the end of createScene. I see you made sure all the event listeners are removed and audio is stopped. That seems fine. It seems you are removing the level scenes correctly also, though I have seen a condition there for removing the previous level. Make sure that gets called when needed.
-
in loading.lua you are calling gotoScene in createScene. True you are calling it with 1000 delay but I think it would be wise to move it to enterScene and reduce the delay accordingly.
-
One issue I think might be getting you memory usage increase when moving to new levels is the fact that all your level files create global variables for the sprites and mybe some other things. This will never be released. Objects will me removed from screen when you remove the group they are in however all the global references will remain and the tables of those objects will remain in memory. Make them local if you don’t need them outside the level module or set some global table to hold them that you can clear when changing levels. Also it might be good to unrequire the modules as that will free up some more memory.
local testmod = require “mymodule”
testmod = nil
package.loaded[“mymodule”] = nil
That’s all I can give you after a quick look. I may have misinterpreted something and I definitely missed some things.
Good luck.
Thanks primoz. I’ll ‘mark solved’ when solved.
Well I followed all these steps now. No luck. -_-
Also I could’t find myself adding enterframe listeners before createScene as told here. It’s only a function for removing enterframe listeners at exitScene.
Note that anything in scene module that is not inside a function gets called as soon as the module is loaded. It’s at the end of mainGameInterface.lua. As I remember it’s not in a function thus will get called as soon as you require mainGameInterface. Also it will only get called once unless you remove the scene with removeScene (you are doing this anyway but still).
Did you make all the variables inside level files local?
Actualy I just remembered something. In your load loading.lua you have this:
if(data.bool == 1) then
storyboard.removeScene(“level”…level-1)
else
storyboard.removeScene(“level”…level)
end
The levels are not scenes. They are just loaded modules and thus if you want to get rid of them you have to unrequire them as I mentioned before. I don’t think storyboard.removeScene will unrequire a module if it is not a scene. This will not dispose of your gloabla variables in the level files so make sure to make them local or insert them in a global table that you can clear here.
Also I’m not sure why the if statement there. What is that supposed to do?
I made all the variables in level1.lua local and then I tested lag by just restarting that level again and again. Lag remained.
What I am doing with the if statement is that I am removing the previous level if its time to go to the next level, else I am removing the current level it its time to restart the same level.
But then now I have used the method u showed to unrequire them instead, with the same if condition, inside the exit scene of mainGameInterface.lua
I see. Well that is what I could come up with by just looking at your code. Short of debugging your app and monitoring memory usage after every step and release when changing levels I can not help you any more.
Ok thanks for you help!