Ephemeron

An ephemeron is a data structure that solves two related problems in garbage collected systems.

When the garbage collector detects such a set, the ephemerons are queued for notification and their keys and values are traced.

In computer science, finalization occurs when a garbage collector (GC) informs an application that an object is "almost collectable".

Seen both as key-value pairs, the main difference between weak references and an ephemerons is the way the garbage collector treats them.

This subtle difference impacts in graphs with some kinds of cycles, where weak pairs do not describe correctly that an object ought to be "almost collectable".

Ephemerons solve a problem which is commonly found when trying to "attach" properties to objects by using a registry.

[3] 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.

An implementation of an OCaml ephemeron type was presented in 2014[6] and added to the standard library in release 4.03.

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.