Java Reference
In-Depth Information
We look at each of these types in the next four sections.
The MEMORY cache is a reference-based cache (see the java.lang.ref Java-
Docs). Each object in the cache is given a reference type. The reference type pro-
vides hints to the garbage collector that let it know how to handle the object.
Like the java.lang.ref package, the MEMORY cache provides WEAK and SOFT
references. Additionally, when specifying the reference type as WEAK or SOFT , the
garbage collector determines what stays and what goes, based on memory con-
straints and/or current access to the cached objects. When you use a STRONG
reference type, the cache is guaranteed to retain the object no matter what until
a flush interval is called.
The MEMORY cache model is ideal for applications that are more interested in
memory management than in object access strategies. Because of the STRONG ,
SOFT , and WEAK reference types, you can determine which results should persist
longer than others. Table 9.7 provides a quick view of how each reference type
functions and how the different types determine the length of time the object is
cached in memory.
Table 9.7
MEMORY cache reference types
The WEAK reference type discards cached objects quickly. This reference type does not pre-
vent the object from being collected by the garbage collector. It merely provides a way to
access an object in the cache that will be removed with the first pass of the garbage collec-
tor. This is the default reference type and works great for keeping your cache occupied with
objects that are accessed on a very consistent basis. Because of the faster rate of discard,
your cache is guaranteed to not exceed memory limits. It is more likely that you will get a
higher rate of database hits with this reference type.
The SOFT reference type is also good for objects that may need to be released when mem-
ory constraints are important. This reference type retains the cached object as long as
memory constraints allow. The garbage collector will not collect this object unless it is deter-
mined that more memory is needed. The SOFT reference is also guaranteed to not exceed
memory limits and will likely have fewer database hits than the WEAK reference type.
The STRONG reference type holds onto cached objects regardless of memory constraints.
Objects stored as STRONG are not discarded until the specified flush interval. The STRONG
cache should be reserved for static, small, regularly accessed objects. This reference type
will increase performance by reducing database hits and runs the risk of running out of
memory if the cache grows too large.
Search WWH ::

Custom Search