An Ephemeron is an object which refers strongly to its contents as long as the
Ephemeron's key is not garbage collected, and weakly from then on. Ephemerons
solve a problem which is commonly found when trying to "attach" properties to
objects by using a registry. When some property should be attached to an
object, the property should (in terms of GC behavior) typically have the life-
time that an instance variable of this object would have. However, this is
complicated by having an external association between the object and its
property such as:
registry ------------ association ------------- object
Here, the registry (a third party) will hold onto the association itself which
would require manual removal from the registry (instead of automated garbage
collection). While this problem can be always be solved in any given concrete
situation by using one of the various weak association types, choosing
the 'right' kind of association depends on a variety of factors some of which
can change dynamically.
Ephemerons solve this problem by defining that the 'contents' (value) of an
Ephemeron will be held strongly until the key is known to be garbage
collected. From then on, the contents of the ephemeron will be held weakly.
Therefore, the contents of an ephemeron can only become eligible for garbage
collection if and only if the key is garbage collectable which is the exact
behavior which we would observe for an instance variable of the object.