The BlackBerry Java Virtual Machine manages memory usage on the smartphone. The BlackBerry JVM allocates memory, performs garbage collection, and automatically swaps data between random access memory and application storage.
BlackBerry Device Software provides a number of tools to help you manage data in your apps. These tools provide ways to back up data, remove sensitive data, remove data that is no longer needed, and free up memory when needed.
Best practice: Minimizing memory use
To minimize runtime memory, consider the following guidelines:
- Use primitive types (such as int or Boolean) instead of objects (such as String or Integer).
- Do not depend entirely on the garbage collector.
- Avoid creating many objects quickly.
- Set object references to null when you are finished using them.
- Reuse objects as much as possible.
- Move heavy processing to the server. For example, you can filter or sort data before sending it to the BlackBerry smartphone.
Removing sensitive data
The memory cleaner can delete sensitive data that is stored in memory on a BlackBerry smartphone. Specific events trigger the memory cleaner to clear various caches and perform secure garbage collection. The memory cleaner is not on by default, but is turned on automatically when you enable encryption. To manually turn on the memory cleaner, the smartphone user clicks Options > Security Options > Advanced Security Options > Memory Cleaning and sets the status to Enabled.
The Memory Cleaner API is implemented in the net.rim.device.api.memorycleaner package.
Users can configure which events trigger a memory cleaning. You can register your app to be notified if one of those events occurs. To do so, implement the MemoryCleanerListener interface and register it using one of the static methods MemoryCleanerDaemon.addListener() or MemoryCleanerDaemon.addWeakListener().
The MemoryCleanerListener interface has two methods, cleanNow() and getDescription(). The cleanNow() method is invoked by the memory cleaner when a user configurable event occurs. The memory cleaner passes an event parameter when it calls cleanNow() to indicate the event that initiated the memory clean request. The getDescription() method is invoked by the memory cleaner if the memory cleaner must display information about the apps that are registered cleaners. This functionality is required, for example, on the Memory Cleaning option screen.