gc
Coil’s heap is garbage collected; most programs never touch this module. Use
it when native code holds on to a Coil value (root it), when a registry
should not keep its entries alive (weak and upgrade), or to measure memory
(heap_bytes, collect).
A class or enum can define fn drop() to release a native resource when its
value becomes unreachable. A collection marks everything reachable, runs the
drop of unreachable values (each at most once), clears Weak handles whose
value is gone, then frees memory. A Weak to an immediate value (an int, a
bool) always upgrades. Root and Weak cannot be sent to another thread.
Built into the compiler.
Import what you use (use path::* is not allowed):
use gc::{Root, Weak};
Types#
| Name | Description |
|---|---|
Root | A strong pin that keeps a value alive, made by root. |
Weak | A handle that refers to a value without keeping it alive, made by weak. |
Functions#
| Name | Description |
|---|---|
root | Pins value so the garbage collector keeps it alive while the returned Root is reachable. |
get | Returns the pinned value, leaving the pin in place. |
unroot | Releases the pin and returns the value it held. |
weak | Makes a handle to value that does not keep it alive. |
upgrade | Returns the value while it is still alive, None once it was collected. |
heap_bytes | Returns the size of the managed heap in bytes. |
collect | Runs a full garbage collection now and returns the number of bytes freed. |