Responding to low-memory conditions

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

When the system runs low on memory, the BlackBerry Tablet OS attempts to reclaim resources by closing background applications. The OS attempts 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 you receive a low-memory event, your application should immediately save state, release resources, and destroy its objects. This way the user's application data will be preserved if your application is terminated. If your application receives focus again, the user expects to return to the application in the state it was in when it was pushed to the background. Your application should detect whether it was terminated due to low memory, then load the saved state if appropriate.

The following code example shows how to detect a low-memory event using the BlackBerry Platform Services (BPS) library:

// Initialize the BPS library
bps_initialize();

//Request and process available Navigator events
navigator_request_events(0)

for(;;) {
     bps_event_t *event = NULL;
     rc = bps_get_event(&event, 0);

     if (event) {
          int domain = bps_event_get_domain(event);

          if (domain == navigator_get_domain()) {
               switch (bps_event_get_code(event)) {
                    case NAVIGATOR_LOW_MEMORY:

                    // Perform low memory handling steps here
                    break;
               }
          }
     } else {
          break;
     }
}