MemoryManagementAPI - leftmike/foment GitHub Wiki

Garbage Collection

Two different collectors are supported.

  • No collection
  • Mark and sweep (default)

Without collection, each thread has it's own memory region (babies) from which it allocates objects. Guardians are not supported.

Using the mark and sweep collector, all objects are allocated from a single memory region (adults) shared by all threads. The mark and sweep collector does support guardians.

Ephemerons

SRFI 124: Ephemerons is fully supported with extensions. (import (srfi 124)) or (import (foment base)) to use these procedures.

procedure: (set-ephemeron-key! ephemeron obj)

Set the key of ephemeron to obj unless ephemeron is broken in which case do nothing.

procedure: (set-ephemeron-datum! ephemeron obj)

Set the datum of ephemeron to obj unless ephemeron is broken in which case do nothing.

Guardians

(import (foment base)) to use these procedures.

procedure: (make-guardian)
procedure: (guardian)
procedure: (guardian obj)

A new guardian will be returned. A guardian is a procedure which encapsulates a group of objects to be protected from being collected by the garbage collector and a group of objects saved from collection. Initially, both groups of objects are empty.

To add an object to the group of objects being protected, call the guardian with the object as the only argument.

(define g (make-guardian))
(define obj (cons 'a 'b))
(g obj)

When a protected object is inaccessible, other than from a guardian, and could be collected, it is moved to the saved group of any guardians protecting it, rather than being collected. To get the objects from the saved group, the guardian is called without any arguments. The objects from the saved group are returned one at a time until the saved group is empty in which case #f is returned.

(g) => #f
(set! obj 123)
(collect)
(g) => (a . b)
(g) => #f

An object may be protected by more than on guardians and more than once by the same guardian. When an object becomes inaccessible it will be moved into saved groups the same number of times that it was protected. Finally, guardians may be protected by guardians.

Garbage Collection

procedure: (collect)

Perform a full collection.

Memory Usage

procedure: (object-counts)

Return a count of objects allocated since the last call to object-counts-reset! or process startup if object-counts-reset! has never been called. The result is a list of pairs, where the car is the name of an object type or a size and the cdr is how many times that type or size has been allocated. Note that the counts are for all threads in the process.

procedure: (object-counts-reset!)

Reset the counts returned by object-counts to zero.

procedure: (stack-used)

Return the maximum number of slots used by the calling thread for stack space, where a slot is 4 bytes on 32 bit systems and 8 bytes on 64 bit systems. Use stack-used-reset! to reset the maximum to zero.

procedure: (stack-used-reset!)

Reset the maximum stack used to zero for the calling thread.

⚠️ **GitHub.com Fallback** ⚠️