Reacting to Low Memory Warnings

Share on Facebook0Share on Google+3Tweet about this on TwitterShare on LinkedIn0

MemoryWhenever your app is taking too much memory, the OS will first issue what’s called a “low memory warning” to give you a chance to do something—such as free up memory—before your app is forced to quit. If your app is forced to quit, from your user’s perspective, your app will have crashed and quit (possibly causing a great deal of frustration).

In today’s tutorial, I’ll show you how to respond to these low memory warnings, and also recommend things you might do to prevent crashes from occurring (and possibly even preventing low memory warnings altogether).

NOTE: Low memory warnings are currently unreliable on the Android platform, so this tutorial will focus mostly on iOS, though the preventative measures will apply to all platforms.

Responding to the Warnings

In order to respond to memory warnings, you need to add a “memoryWarning” event listener to the Runtime object. For the sake of this tutorial, we’ll assume you’re doing so in main.lua.

Runtime:addEventListener( "memoryWarning", onMemoryWarning )

If you ever want to stop listening to “memoryWarning” events, you need to explicitly remove the event listener, like so:

Runtime:removeEventListener( "memoryWarning", onMemoryWarning )

Remember this, especially if you add the event listener in a module other than main.lua (that may be unloaded at some point, preventing access to the listener function you specify).

The Listener

Now, what you actually put into the ‘onMemoryWarning’ function is up to you—because only you know the exact details of your app—but here are some things you can do:

  • Unload as many unneeded objects as possible.
  • Stop any event listeners (especially ‘enterFrame’ listeners) that you do not absolutely need.
  • Purge and/or remove any scenes that aren’t currently active (try: storyboard.removeAll()).
  • As a rule of thumb, get rid of anything you don’t need.

Here’s an example:

local function onMemoryWarning( event )
audio.dispose( someAudioHandle )
Runtime:addEventListener( "memoryWarning", onMemoryWarning )

Preventing Memory Warnings

The best thing you can do is to try your best to prevent memory warnings from occurring in the first place. Once again, I can’t tell you exactly what to do, because you’re the “driver behind the wheel” when it comes to developing your app, but I can give you some ideas.

  • Prior to changing into a heavy scene, purge all scenes (or purge all scenes prior to creating your scene). See: storyboard.purgeAll()
  • If you’re working with multiple platforms and devices, check if the device is a lower-performance device and load a different set of graphics, or load less “cosmetic” objects than you want.
  • Decide on what’s needed, and what’s extra—trim the fat.
  • Review your code, line-by-line to ensure there are no memory leaks.

Of course, there are many more things you can do that are specific to your app only, but you get the idea. The point is to free up as many resources as possible either prior to a memory warning occurring, and especially during a “memoryWarning” event so the OS does not have to kill your app.

Share on Facebook0Share on Google+3Tweet about this on TwitterShare on LinkedIn0

This entry has 5 replies

  1. Thank you @Jon,
    Thank you Corona Labs Team!

    It`s a really important subject for us to take care of surely. Nice to see this tuto coming up.


  2. Steve says:

    You may also want to save the game state, just in case the game does happen to crash. If nothing else, a player will reopen the app and not lose their recent gold, unlocked levels, current page, and so forth.

    It’s frustrating for an app to crash while you’re doing something, but more frustrating when it sets you back minutes or hours (or resets the game entirely!).

  3. David says:

    Nice..! This is a very helpful tutorial.

    On a similar note, I’d love to see a memory usage feature in a future version of the Corona Simulator. As far as a tool, all it really does is simulate the device, and it has no real developer add-ons. Adding things like the automatic ability view memory usage without having to write code would be so excellent and helpful.


  4. owenyang says:

    that’s helpful,thanks

  5. David — although you need to copy and paste the code . . . there’s a great little widget for showing Memory usage (and FPS) by Wizem that was posted over 2 years ago. It’s here at: