||This article needs attention from an expert in Computer science. The specific problem is: failure to describe important details. (January 2013)|
- For other uses, see Ephemera (disambiguation).
In computer science, finalization occurs when a garbage collector (GC) informs an application that an object is "almost collectable." It is used to help an application maintain its invariants. Weak references may be used by a garbage collector to determine the objects that are almost collectable.
However, weak references are not sufficient to describe all situations where an object ought to be almost collectable. For example, key-value pairs may be stored using weak pairs (where the keys are weak references). If a key maps to itself as a value, however, the key cannot be collected. Ephemerons are similar to weak pairs, but an object in an ephemeron's key field may be classed as "almost collectable" even if it is reachable from the ephemeron's value fields.
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:
property --------- 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 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 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.
Ephemerons were first invented by George Bosworth while he worked at Digitalk. They were used as the finalization mechanism in Visual Smalltalk Enterprise. Today ephemerons are available in most Smalltalk dialects as well as many other languages with automatic garbage collection.
Examples of use
The Racket dialect of Lisp has support for ephemerons in its runtime system. There, ephemerons are used in combination with weak mappings to allow the garbage collector to free key-value pairs even if the value holds a reference to a key.
Lua does not contain a separate ephemeron construct, but its table data structures may be set to holds its keys, values, or both in a weak fashion. If the keys are held weakly, but values are held strongly, the table will act like an ephemeron.
- Barry Hayes (1997). "Ephemerons: A New Finalization Mechanism". Object-Oriented Languages, Programming, Systems, and Applications.
- "Special objects - GNU Smalltalk User's Guide". Retrieved 2013-02-20.
- "Ephemerons". Retrieved 2013-02-20.
- "15.2 Ephemerons". Retrieved 2013-02-20.
- "Lua 5.2 Reference Manual". Retrieved 2013-02-20.
- ".NET 4.0 - System.Runtime.CompilerServices.ConditionalWeakTable". IKVM.NET Weblog. Retrieved 2013-10-14.