I was wondering how do you clear your parent groups to prevent memory leaks?
As a beginner, I was thinking :
- Get all children of a parent sprite (something that might be achievable thanks to the snippet shared by @MikeHart
- removeChild -> But was does 'remove' really mean in the documentation? Should I 'nil' the objects also?
- Get all listeners that belong to the parent and its children, and remove it (will need to search how to get those listeners)
Am I on the good way? How do you handle that task?
If somebody has links to give me so that I can study some code (snippets), that would be very helpful.
Thank you, community.
Comments
Well object is garbage collected (aka removed from memory) when there a no references to this object.
When you add object to a parent, you create a reference to this object, so what remove function does is that it removes this references.
You only need to nil in cases, where you created your own reference.
For example:
Additionally, if, for example, a parent doesn't have any more references, like in case with scenes, but it still has children, then parent and all children are garbage collected, again you don't have to do anything.
I guess only stage is the one left, that is never removed.
Thank you very much!
Edit :
@ar2rsawseen
Only thing when you should remove event listener is when you want to only remove this listener without removing sprite.
1. What does this number represent? What is the unit?
2. What is a decent memory usage, what is considered as too much for the iPad?
3. Can I find somewhere recommended numbers (printed by garbagecollection("count") per app style (ex : interactive book avg -> ?)
4. Are there other metrics that I should track in order to keep my app optimized?
Right now I am using the scene manager with 2048x1536 images (plus a few layers) and my app displays ~350. Already too much?
Any help would be great,
Thank you.
The other metric for app optimisation (and probably more important from a users perspective) is frame rate, by default Gideros can work at either 30 or 60 frames per second (fps), there are plenty of examples of how to calc and display your app's framerate in the forum, most app's wont achieve a solid 30 or 60 fps, but the key is to make something feel as smooth and as responsive to the user as possible.
Optimising for speed is usually counter productive to optimising for memory, as you can often pre-calculate items to make access faster at the expense of memory, I'd concentrate more on optimising for speed / responsiveness rather than memory and then only when you feel you need it. A lot of times the best optimisations can occur at the logic level rather than little "tricks" to make code run a bit faster, as often there is a "better" way to do something, however that said - employing good lua programming practices (keeping references to library functions as locals, not declaring locals inside loops, watching for excess operations / calculations being performed (especially inside nested loops) should ideally be the goal of any conscientious professional programmer.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
I don't have much experience but I don't think 350 is a lot. In the beginning I was stressing about managing memory and everything but then I realized I wasn't even using very much and I was spending a ton of time worried about 50kb or something when a single texturepack can be many mb of memory :P
I tried to stop worrying so much and just watch out for memory leaks and the actual speed of my app so I could get things done faster.
About removing sprites @ar2rsawseen, I have another question that probably looks simple to developers here but this is ok if I look like a beginner, I will ask :
anotherGroup = Group
does not make a copy of Group.
There has only been one memory allocation - local Group = Sprite.new() - that allocates memory.
anotherGroup is only pointing at the original memory block.
For example, try this code (you'll have to make your own dog.png image):
Secondly, I don't think it's good practice to create variables with an initial capital letter as in:
You see, Sprite is just a class definition - no memory has been allocated for Sprite. It's not until you say
So if I understand well :
So the important thing is the memory allocation and having one way (or more) stored to point to it later.
My last question is about what's happening behind the scenes :
I don't know the answer to the second. I do know that the first one "feels" as if it's better code. Because you do something, then reverse it out.
if you run this
cheers
evs
Likes: chipster123
@Caroline yes, it was in a class, things are working fine now.
@evs I see, I am a still a bit confused with the garbage collector, what remains in memory when the stage is cleared, function scope, etc... so your post was helpful.
I was still evaluating Gideros until now, and I have decided to go with it for my first set of 2 applications.
Thank you Gideros Team & community
Likes: gorkem, Caroline, phongtt, chipster123