20. Weak referencesΒΆ

A weak reference is a reference that does not keep the block it refers to alive.

The open source MPS supports weak references only:

  1. in roots that are registered with rank mps_rank_weak();
  2. in objects allocated on an allocation point in a pool of class AWL (Automatic Weak Linked) that was created with rank mps_rank_weak().


If you need more general handling of weak references, contact us.

When the MPS determines that a block is only kept alive by one or more weak references, it may choose to splat those references by replacing them with null pointers when they are fixed. When all weak references to the block have been splatted, the block may be reclaimed.

For example, a scan method for objects in an AWL pool might look like this:

mps_res_t obj_scan(mps_ss_t ss, mps_addr_t base, mps_addr_t limit)
    MPS_SCAN_BEGIN(ss) {
        while (base < limit) {
            obj_t obj = base;
            mps_addr_t p = obj->ref;
            if (MPS_FIX1(ss, p)) {
                mps_res_t res = MPS_FIX2(ss, &p);
                if (res != MPS_RES_OK) return res;
                if (p == NULL) {
                    /* reference was splatted */
                obj->ref = p;
            base += sizeof(*obj);
    } MPS_SCAN_END(ss);
    return MPS_RES_OK;

A reference that passes the “interesting” test MPS_FIX1() can’t be a null pointer, so if the reference is discovered to be null after calling MPS_FIX2() then it must have just been splatted.


Because weak references are splatted when they are fixed, not all weak references to a block are splatted at the same time. Depending on the decisions the MPS makes about which objects to scan, a weak reference may live on for some time after other weak references to the same block have been splatted.


A common way in which weak references are used in programming languages is in weak-key and weak-value hash tables. A weak-key hash table contains weak references to its keys: when it detects that a key has been splatted, it deletes the corresponding value. The AWL (Automatic Weak Linked) pool class supports this by allowing you to specify for each object, a dependent object which may be written to by the scan method. See Dependent objects.


Weak references do not prevent blocks from being finalized. At the point that a block is finalized, weak references will still validly refer to the block. The fact that a block is registered for finalization prevents weak references to that block from being splatted. See Finalization.

Previous topic

19. Telemetry

Next topic

21. Plinth


All MPS Kit releases


All open issues

Contact us

#memorypoolsystem on irc.freenode.net