### Navigation

- index
- next |
- previous |
- Memory Pool System 1.111.0 documentation »
- Reference »

# 10. Garbage collection

## 10.1. Generation chains

A generation chain describes the structure of the generations in a set of automatically managed pools. The same generation chain should be used for all pools whose blocks live and die together.

Create a generation chain by preparing an array of `mps_gen_param_s` structures giving the capacity (in kilobytes) and predicted mortality (between 0 and 1) of each generation, and passing them to `mps_chain_create()`.

When the size of the generation exceeds the capacity, the MPS will be prepared to start collecting the generation. See Scheduling of collections below.

For example:

    mps_gen_param_s gen_params[] = {
        { 1024, 0.8 },
        { 2048, 0.4 },
    };

    mps_chain_t chain;
    mps_res_t res;
    res = mps_chain_create(&chain, arena,
                           sizeof(gen_params) / sizeof(gen_params[0]),
                           gen_params);
    if (res != MPS_RES_OK) error("Couldn't create chain");

`mps_chain_t`

  The type of generation chains. A generation chain describes the structure of generations in a pool.

`mps_gen_param_s`

  The type of the structure used to specify a generation in a generation chain.

      typedef struct mps_gen_param_s {
          size_t mps_capacity;
          double mps_mortality;
      } mps_gen_param_s;

  `mps_capacity` is the capacity of the generation, in kilobytes. When the size of the generation exceeds this, the MPS will be prepared to start collecting it.

  `mps_mortality` is the predicted mortality of the generation: the proportion (between 0 and 1) of blocks in the generation that are expected to be dead when the generation is collected.

  These numbers are hints to the MPS that it may use to make decisions about when and what to collect: nothing will go wrong (other than suboptimal performance) if you make poor choices. See Scheduling of collections.

mps_res_t `mps_chain_create`(mps_chain_t *chain_o, mps_arena_t arena, size_t gen_count, mps_gen_param_s *gen_params)

  Create a generation chain.

  `chain_o` points to a location that will hold a pointer to the new generation chain.

  `arena` is the arena to which the generation chain will belong.

  `gen_count` is the number of generations in the chain.

  `gen_params` points to an array describing the generations.

  Returns `MPS_RES_OK` if the generation chain is created successfully, or another result code if it fails.

  The generation chain persists until it is destroyed by calling `mps_chain_destroy()`.

void `mps_chain_destroy`(mps_chain_t chain)

  Destroy a generation chain.

  `chain` is the generation chain.

  It is an error to destroy a generation chain if there exists a pool using the chain. The pool must be destroyed first.

## 10.2. Scheduling of collections

Note

It’s likely that the algorithm the MPS uses to schedule its collections will change in future releases. There’s a lot of room for improvement here.

The new size of a generation is the total size of the newly allocated (in generation 0) or newly promoted (in other generations) blocks in that generation. These are the blocks that have not been condemned since they were allocated or promoted into this generation. In pools like AMC (Automatic Mostly-Copying) where the survivors get promoted to the next generation in the chain, the new size of each generation (other than the topmost) is the same as its total size, but in pools like AMS (Automatic Mark and Sweep) where survivors do not get promoted, the two sizes can be different.

The first generation in a pool’s chain is the nursery space. When the nursery’s new size exceeds its capacity, the MPS considers collecting the pool. (How long it takes to get around to it depends on which other collections on other pools are in progress.)

Note

You can affect the decision as to when to collect the nursery space by using the ramp allocation pattern.

If the MPS decides to collect a pool at all, all generations are collected below the first generation whose new size is less than its capacity.

In pools such as AMC (Automatic Mostly-Copying), blocks in generation g that survive collection get promoted to generation g+1. If the last generation in the chain is collected, the survivors are promoted into an arena-wide “top” generation.

The predicted mortality is used to estimate how long the collection will take, and this is used in turn to decide how much work the collector will do each time it has an opportunity to do some work. The constraints here are:

1. The client program might have specified a limit on the acceptable length of the pause if the work is being done inside `mps_arena_step()`.
2. The collector needs to keep up with the client program: that is, it has to collect garbage at least as fast as the client is producing it, otherwise the amount of garbage will grow without bound.

With perfect prediction, the collector’s work should be smoothly distributed, with a small maximum pause time. Getting the predicted mortality wrong leads to “lumpy” distribution of collection work with a longer maximum pause time. If the predicted mortality is too high, the collector will start out by taking small time slices and then find that it has to catch up later by taking larger time slices. If the predicted mortality is too low, the collector will take larger time slices up front and then find that it is idle later on.

## 10.3. Garbage collection start messages

mps_message_type_t `mps_message_type_gc_start`(void)

  Return the message type of garbage collection start messages.

  Garbage collection start messages contain information about why the garbage collection started.

  The access method specific to a message of this message type is:

  - `mps_message_gc_start_why()` returns a string that describes why the garbage collection started.

  See also

  Messages.

const char *`mps_message_gc_start_why`(mps_arena_t arena, mps_message_t message)

  Return a string that describes why the garbage collection that posted a message started.

  `arena` is the arena which posted the message.

  `message` is a message retrieved by `mps_message_get()` and not yet discarded. It must be a garbage collection message: see `mps_message_type_gc()`.

  Returns a pointer to a string that is describes (in English) why this collection started. The contents of the string must not be modified by the client. The string and the pointer are valid until the message is discarded with `mps_message_discard()`.

  See also

  Messages.

## 10.4. Garbage collection messages

mps_message_type_t `mps_message_type_gc`(void)

  Return the message type of garbage collection statistic messages.

  Garbage collection statistic messages are used by the MPS to give the client program information about a garbage collection that has taken place. Such information may be useful in analysing the client program’s memory usage over time.

  The access methods specific to a message of this type are:

  - `mps_message_gc_live_size()` returns the total size of the condemned set that survived the garbage collection that generated the message;
  - `mps_message_gc_condemned_size()` returns the approximate size of condemned set in the garbage collection that generated the message;
  - `mps_message_gc_not_condemned_size()` returns the approximate size of the set of objects that were in collected pools, but were not condemned in the garbage collection that generated the message.

  See also

  Messages.

size_t `mps_message_gc_condemned_size`(mps_arena_t arena, mps_message_t message)

  Return the “condemned size” property of a message.

  `arena` is the arena which posted the message.

  `message` is a message retrieved by `mps_message_get()` and not yet discarded. It must be a garbage collection message: see `mps_message_type_gc()`.

  The “condemned size” property is the approximate size of the condemned set in the garbage collection that generated the message.

  See also

  Messages.

size_t `mps_message_gc_live_size`(mps_arena_t arena, mps_message_t message)

  Return the “live size” property of a message.

  `arena` is the arena which posted the message.

  `message` is a message retrieved by `mps_message_get()` and not yet discarded. It must be a garbage collection message: see `mps_message_type_gc()`.

  The “live size” property is the total size of the set of objects that survived the garbage collection that generated the message.

  See also

  Messages.

size_t `mps_message_gc_not_condemned_size`(mps_arena_t arena, mps_message_t message)

  Return the “not condemned size” property of a message.

  `arena` is the arena which posted the message.

  `message` is a message retrieved by `mps_message_get()` and not yet discarded. It must be a garbage collection message: see `mps_message_type_gc()`.

  The “not condemned size” property is the approximate size of the set of objects that were in collected pools, but were not in the condemned set in the garbage collection that generated the message.

  See also

  Messages.

### Navigation

- index
- next |
- previous |
- Memory Pool System 1.111.0 documentation »
- Reference »