Respond to low memory conditions

Low memory conditions occur when the BlackBerry device user has many memory-intensive applications open and running in the background. If applications do not suspend processes when moved to the background, they might hold memory unnecessarily.

When the system runs low on memory, the BlackBerry 10 OS tries to reclaim resources by terminating background applications. The BlackBerry 10 OS tries to close the application that has been in the background the longest, and continues to close applications until it reclaims enough memory for normal system operation.

When the BlackBerry 10 OS tries to terminate an application due to a low memory condition, it terminates the application's processes and destroys the associated NativeApplication instance. However, the application window remains in the list of background applications. From the user's perspective, the application appears to be running in the background. When the user returns the application to the foreground, the BlackBerry 10 OS starts the application and creates an instance of NativeApplication.

When the system runs low on memory, the QNXApplication class dispatches the QNXApplicationEvent.LOW_MEMORY event. You should create a listener to respond to this event. When your application receives a LOW_MEMORY event, it should save the application's state, release resources, and destroy its objects. This way the user's application data can be preserved if your application is terminated.

If your application receives focus again, it can detect whether it was terminated due to a low memory condition, then load its saved state.

Listen for a low memory condition

How to

Listen for a low memory event.

Solution

Set up an event listener to listen for the QNXApplicationEvent.LOW_MEMORY event.

addEventListener(QNXApplicationEvent.LOW_MEMORY, onLowMemory);

Define an onLowMemory function that your app calls when a low memory condition occurs.

public function onLowMemory(event:QNXApplicationEvent):void
   {     
      trace("Warning: Memory is Low");
      // Save state
      // Release resources
      // Destroy its objects
   }

Build requirements

import qnx.events.*;
// You must add QNXDevice.ane to your application

Discussion

Use the onLowMemory function to respond to the low memory event.

You need to add the QNXDevice AIR native extension to your project. For more information on adding AIR native extensions, see AIR native extensions.

You can use trace comments to test and troubleshoot your application. The debug console in Adobe Flash Builder or Powerflasher FDT displays trace comments when testing your application in the debug configuration.

comments powered by Disqus