Highly effective memory optimization suggestion for games with more than one scene

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

Comments

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    I've exchanged messages with codewizard about this and they are working on that. They had to scrap their custom memory allocator because of crash issues and returned to the default LUAjt one. Once they get the creator stabilized the will redo their memory system.
  • HoneyTribeStudiosHoneyTribeStudios Member Posts: 1,792
    I just released an update to a game that used to be 1 scene. It's now 2 scenes and loads of people are saying it is crashing. Which is very strange. I thought that changing scene should dump the memory used too.

    Maybe @BlackCloakGS or @CodeWizard could shed some light on memory usage with scene changes?
  • SnapFireStudiosSnapFireStudios Member Posts: 1,603
    Interesting.. I've always tried to avoid using destroy actor.
    - Thomas
  • CodeWizardCodeWizard Inactive, Chef Emeritus Posts: 1,143
    Hmm. Good find. Noted for follow up.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Thanks for the advice about destroying actors... I may try that out.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • darrelfdarrelf Member Posts: 243
    edited September 2013
    I know the original post is a few months old but does the latest Nightly Build include a new memory system?

    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!

  • Ali_WoodAli_Wood Member Posts: 52
    This is an excellent post @SixFeetOfMeat. I have been optimising my game early on where i only have 2 scenes before cracking on with the rest of the game. I noticed using Instruments that my RAM usage was incredibly high the first time i loaded level 1 but then when i played it again it would be around 60% less!

    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
  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    Woo nice post. If my game crashes when testing I will be sure to try this out! Thanks
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited January 2014

    @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

Sign In or Register to comment.