Highly effective memory optimization suggestion for games with more than one scene
SixFeetOfMeat
Member Posts: 4
Hi All,
I'm working on an update for Sums Academy (www.3d6games.com/sums) and I just had a revelation - Changing a scene does not release the memory (RAM allocation) that the scene was using. When the app enters the next scene, most or all of the memory for the previous scene still resides in RAM.
I used Instruments to track the RAM allocation on iPad 1. I found that as I progressed through the title scene (26MB RAM allocated) to the level selection scene (increase to around 65MB RAM allocated) to the game level scene (increased to around 105MB RAM allocated) the RAM allocation just kept climbing. When I returned to the level selection scene the RAM remained quite high (around 95MB+ RAM allocated).
Why did the RAM not decrease more when I returned to the level selection scene? Why was the game requiring so much RAM allocation in general?
I decided to experiment by editing the title scene so it would skip the level selection scene and go straight into the game scene. When I tested it I saw RAM allocation for the game scene was just 75MB! That's 30MB less than when I accessed the game scene previously. This led me to ask - Was a large chunk of the level selection scene remaining in memory, even though I had changed scenes to the game scene?
I then decided to try destroying everything in the current scene before changing the scene. I added a rule to every actor in every scene that destroyed the actor shortly before the scene change occurred. I also added a timer to the scene change routine to allow the actors time to die off before the actual scene change happened (half a second). This resulted is a completely black screen just before the scene change...assuring me that everything had been destroyed.
The result - the RAM allocation for every scene beyond the first went down...a lot.
Title scene remained at 26MB RAM allocated, the level selection scene hit around 60MB RAM allocated and the game capped at at 84MB RAM allocated. When I returned to the level selection scene the RAM returned to the 65MB range.
So while I'm not recapturing 100% of the RAM allocated for the scenes, I am recapturing quite a bit by taking the time to purge the actors before changing the scene.
One other funny side observation, the loads between the scenes were notably faster when destroying the actors (loads between scenes went from 4.25 seconds to 3.75 seconds).
I theorize that it would be best to remake the entire game in one scene. But for the time being I'm going to finished this round of optimizations, clean up the transitions with some art and update the universal binary to support the iPhone 4 & 5 so I can add a few more lesson packs to the game soon.
Best,
- Six
I'm working on an update for Sums Academy (www.3d6games.com/sums) and I just had a revelation - Changing a scene does not release the memory (RAM allocation) that the scene was using. When the app enters the next scene, most or all of the memory for the previous scene still resides in RAM.
I used Instruments to track the RAM allocation on iPad 1. I found that as I progressed through the title scene (26MB RAM allocated) to the level selection scene (increase to around 65MB RAM allocated) to the game level scene (increased to around 105MB RAM allocated) the RAM allocation just kept climbing. When I returned to the level selection scene the RAM remained quite high (around 95MB+ RAM allocated).
Why did the RAM not decrease more when I returned to the level selection scene? Why was the game requiring so much RAM allocation in general?
I decided to experiment by editing the title scene so it would skip the level selection scene and go straight into the game scene. When I tested it I saw RAM allocation for the game scene was just 75MB! That's 30MB less than when I accessed the game scene previously. This led me to ask - Was a large chunk of the level selection scene remaining in memory, even though I had changed scenes to the game scene?
I then decided to try destroying everything in the current scene before changing the scene. I added a rule to every actor in every scene that destroyed the actor shortly before the scene change occurred. I also added a timer to the scene change routine to allow the actors time to die off before the actual scene change happened (half a second). This resulted is a completely black screen just before the scene change...assuring me that everything had been destroyed.
The result - the RAM allocation for every scene beyond the first went down...a lot.
Title scene remained at 26MB RAM allocated, the level selection scene hit around 60MB RAM allocated and the game capped at at 84MB RAM allocated. When I returned to the level selection scene the RAM returned to the 65MB range.
So while I'm not recapturing 100% of the RAM allocated for the scenes, I am recapturing quite a bit by taking the time to purge the actors before changing the scene.
One other funny side observation, the loads between the scenes were notably faster when destroying the actors (loads between scenes went from 4.25 seconds to 3.75 seconds).
I theorize that it would be best to remake the entire game in one scene. But for the time being I'm going to finished this round of optimizations, clean up the transitions with some art and update the universal binary to support the iPhone 4 & 5 so I can add a few more lesson packs to the game soon.
Best,
- Six
Comments
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Maybe @BlackCloakGS or @CodeWizard could shed some light on memory usage with scene changes?
- Thomas
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
My current Universal Build project is driving me crazy. I've done around 7-8 passes to optimize my code, have simplified where I can, have ordered the code in the best way (I've been watching @FryingBaconStudios videos :-) ) and yet my relatively small game is crashing an iPod 4th gen. It runs without crashing on my iPad 2 and iPhone 5 although a 0.1 second countdown timer (not GS's built-in one) is very slightly hesitant every now and again.
I've studied the real memory usage with Activity Monitor in Instruments and I see the memory simply increases every time I cycle through 4 initial tutorial scenes - ON ALL 3 DEVICES. These are simple instruction scenes with no rules other than when touch is pressed, change scene.
So my questions are:
- do the latest builds include the memory fix so I should be seeing memory usage decrease when changing scene (thereby indicating my code could be corrupt)?
- if not, is a new memory system planned for the next Nightly/Creator releases?
- should I do something else e.g. look at destroying everything before changing scenes to see if that helps?
Many many many thanks for any advice!
This baffled me as surely if it's loading the exact same scene the numbers should be very similar!
After reading your post i realised that Level 1 destroy's most actors at the end of the level but my main menu doesn't. So maybe destroying everything is the Key
I'll try this now and fingers crossed it speeds my load times up too
Thanks again
GSINVENTION FREE TEMPLATES
@SixFeetOfMeat Interesting: I really thought that problem had been solved in the latest GSC... anyhow, I like this idea too; I'll definitely try the destroying thing in my latest game to see if it stops it crashing on an iPad 1. A simple idea that's easy to overlook, Cheers. :-)
PS As @FryingBaconStudios mentioned, hopefully the memory build-up prob. will be defeated once and for all, which'll be good.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps