Using the Garbage Collector
The BlackBerry Java Virtual Machine includes the Garbage Collector, which runs periodically to remove unreferenced and weakly referenced objects from memory. Do not call the Garbage Collector directly, but release resources by setting their reference to null after use.
Full garbage collection on a smartphone
The full garbage collection operation executes for 1 second on average and should take less than 2 seconds to complete. The full garbage collection operation performs the following actions:
- Performs a RAM garbage collection operation.
- Marks objects in flash memory that are no longer referenced or no longer persisted.
- Releases any nonpersistent object handles in RAM and flash memory.
The system might initiate a full garbage collection operation in the following situations:
- The BlackBerry Java Virtual Machine cannot allocate an object because of a lack of available space in RAM.
- A process is about to exceed its currently allocated heap size.
- The BlackBerry JVM cannot allocate a new object because the object handles are not available.
- The BlackBerry smartphone is idle.
RAM garbage collection on a smartphone
The BlackBerry Java Virtual Machine initiates a RAM garbage collection operation only when the BlackBerry JVM cannot allocate an object because of a lack of space in RAM. The RAM garbage collection operation typically takes 500 to 600 milliseconds to execute. The garbage collection operation removes any freshly allocated variables that are no longer referenced in RAM. To make sure that the lack of a reference in RAM is a sufficient condition for removing the object, a RAM garbage collection operation can only be performed when objects have not been paged out to flash memory.
Idle garbage collection on a smartphone
Garbage collection does not occur every time the smartphone idles. It occurs only when the system considers a garbage collection operation to be beneficial for optimal system performance and maximized battery performance.
To improve performance without impacting the user experience, the system attempts to perform the following garbage collection operations when the smartphone idles:
- A full garbage collection operation can occur when the smartphone idles for a relatively short period of time.
- A thorough garbage collection operation can occur when the smartphone idles for a significant period of time.