The creation of objects can be expensive so an object cache allows the toolkit to reuse certain objects instead of creating, destroying, and then recreating them. The toolkit provides a object cache each for formats and processors. An object cache is a special region of memory used to temporarily store these objects. The toolkit saves the reusable objects in the cache after using them and can then load and re-initialize them from the cache when needed. The reusable objects must implement the Cacheable interface.
The toolkit implements each object cache as a Hashtable that maps the name of the cacheable object to a Vector that contains instances of the cacheable object. This implementation means that the size of an object cache can increase both in the number of types stored in it and in the number of instances of the types.
An object cache grows each time the externalizer caches an object that does not already exist in the cache. The toolkit creates an entry in the cache's Hashtable with the name of the object type as the key and the Vector that contains the object instance as the value. This increases the size of the object cache by one type.
An object cache grows by instances when the Vector (identified by name of the objects it contains) does not contain sufficient instances to fulfill the externalizer's getFromCache request. Normally, the Vector returns the last element that was added, which causes the getFromCache method to remove the element from the Vector. If the Vector is empty because of concurrent requests for an instance, the externalizer creates one. This type of growth occurs when the externalizer returns one or more of the concurrent instances to the cache.
The toolkit uses two mechanisms to limit the size of object caches. The first mechanism is a thread that operates in the background to remove expired objects from the cache. The toolkit assigns a timestamp to each object as it is placed in the cache. The thread compares each object's timestamp to the current timestamp and if the difference between the two exceeds a configurable limit, the thread removes the object from the cache. How often the thread performs the comparison is also configurable. Both configuration settings are in the toolkit definition (btt.xml) file.
The second mechanism consists of a set of settings in the toolkit definition file that control the number of objects stored in the cache. These setting specify the maximum number of types, the maximum number of instances for each type, and overall number of types and instances stored each object cache. See Limiting the size of an object cache.