Java Reference
In-Depth Information
A NOTE ON TERMINOLOGY
Discussing weak and soft references can be confusing because so much of the terminology uses
similar words. Here's a quick primer on that terminology:
Reference
A reference (or object reference) is any kind of reference: strong, weak, soft, and so on. An
ordinary instance variable that refers to an object is a strong reference.
Indefinite reference
This is the term I use to distinguish between a strong reference and other, special kinds of ref-
erences (e.g., soft or weak). An indefinite reference is actually an instance of an object (e.g.,
an instance of the SoftReference class).
Referent
Indefinite references work by embedding another reference (almost always a strong reference)
within an instance of the indefinite reference class. The encapsulated object is called the refer-
ent.
Still, to many programmers this “feels” different. In fact, even the terminology reflects that:
no one speaks of “caching” a thread for reuse, but we will explore the reuse of indefinite ref-
erences in terms of caching the result of database operations.
The advantage to an indefinite reference over an object pool or a thread-local variable is that
indefinite references will be (eventually) reclaimed by the garbage collector. If an object pool
contains the last 10,000 stock lookups that have been performed and the heap starts running
low, the application is out of luck: whatever heap space remains after those 10,000 elements
are stored is all the remaining heap the application can use. If those lookups are stored via in-
definite references, the JVM can free up some space (depending on the type of reference),
giving better GC throughput.
The disadvantage is that indefinite references have a slightly greater effect on the efficiency
of the garbage collector. Figure 7-7 shows a side-by-side comparison of the memory used
without and with an indefinite reference (in this case, a soft reference).
Search WWH ::




Custom Search