blackberry/WebWorks — Issue #78: Hourglass Improvements
Author: tneil
Created: 2011-12-02 12:18:18+00:00
State: open
Labels: Feature

## Background

In the v2.2 release of WebWorks we implemented a "Memory Maid" which is a background running thread that looks to see if total memory usage has increased by 5% and if so we would call a System.gc()

The reason behind this is that there are two main culprits left that cause a memory leak on BB6/BB7 that is in the operating system mechanism that provides an IPC channel between WebKit and Java.

The Memory Maid was a stop gap measure to ensure that memory was being cleaned up on the Native side of things. Essentially memory was orphaned when the URL of the WebView changed or when the application exited

There are two main items left that require updates so that we work around the memory issues on BB6/BB7

* One is the [Navigation Mode](https://github.com/blackberry/WebWorks/issues/49) updates that will be included in v2.3
* The second is the handling of callback methods from APIs providing function pointers from Java to JavaScript. This is intended to be resolved in v2.4

## Hourglass Improvements

The focus of this feature is to adjust the Memory Maid so that it is less intrusive to a user using the application. The changes to the Memory Maid are as follows

* Threshold of total memory increase has been bumped to 10% instead of the previous 5%
* Memory Maid starts working **after** the first page load. Currently simply loading the first page of your app can exceed the threshold of 5% causing a GC right when your app starts. This is not good
* Memory Maid only starts looking for threshold changes **if** your application changes the URL of the WebView. If your application is an AJAX style application, Memory Maid will not run. Leaks only happen when the URL of the page changes so we shouldn't penalize apps that use an AJAX style screen changing mechanism
* Previously Memory Maid would check for thresholds every time a resource was loaded into a page which caused delays during use of an application when all we really care about is if the thresholds are met after a page change
* A System.gc() is called every time the application exits. This ensures that all memory is cleaned up for the application no matter what

This provides a much better user experience considering the delays of GC only happen during the exit of your application and not during the application usage