#garbage #collection #gc #mark #sweep


An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection

7 releases

0.3.2 Oct 23, 2022
0.3.1 Oct 22, 2022
0.3.0 Mar 24, 2020
0.2.0 Mar 23, 2020
0.1.3 Mar 23, 2020

#239 in Memory management

Download history 10/week @ 2023-07-25 6/week @ 2023-08-01 9/week @ 2023-08-08 20/week @ 2023-08-15 12/week @ 2023-08-22 15/week @ 2023-08-29 21/week @ 2023-09-05 8/week @ 2023-09-12 8/week @ 2023-09-19 9/week @ 2023-09-26 16/week @ 2023-10-03 20/week @ 2023-10-10 19/week @ 2023-10-17 21/week @ 2023-10-24 19/week @ 2023-10-31 13/week @ 2023-11-07

77 downloads per month


400 lines


An ergonomic tracing garbage collector that supports mark 'n sweep garbage collection.

Cargo Documentation License


  • Ergonomic API
  • Mark and sweep heap cleaning
  • Easy (and safe) mutation of heap values, despite cycles
  • Zero-cost access to heap objects through handles


use broom::prelude::*;

// The type you want the heap to contain
pub enum Object {

// Tell the garbage collector how to explore a graph of this object
impl Trace<Self> for Object {
    fn trace(&self, tracer: &mut Tracer<Self>) {
        match self {
            Object::Num(_) => {},
            Object::List(objects) => objects.trace(tracer),

// Create a new heap
let mut heap = Heap::default();

// Temporary objects are cheaper than rooted objects, but don't survive heap cleans
let a = heap.insert_temp(Object::Num(42.0));
let b = heap.insert_temp(Object::Num(1337.0));

// Turn the numbers into a rooted list
let c = heap.insert(Object::List(vec![a, b]));

// Change one of the numbers - this is safe, even if the object is self-referential!
*heap.get_mut(a).unwrap() = Object::Num(256.0);

// Create another number object
let d = heap.insert_temp(Object::Num(0.0));

// Clean up unused heap objects

// a, b and c are all kept alive because c is rooted and a and b are its children

// Because `d` was temporary and unused, it did not survive the heap clean

Who this crate is for

  • People writing dynamically-typed languages in Rust that want a simple, reliable garbage collector
  • People that want to have complex graph data structures with mutation and cycles but who don't want memory leaks

Who this crate is not for

  • People that want garbage collection when writing ordinary Rust code


This crate makes no specific promises about performance. It is designed with a 'best attempt' approach; this means that it should be fast enough for most purposes but is probably not competitive with garbage collectors that have had years of development work ploughed into them.


There are a few things I want to do with broom if I get the time:

  • Smarter cleanup strategies than mark 'n sweep
  • Partial cleans to prevent garbage collection lag spikes

If you're interested in working on any of these things, feel free to open a pull request!


Broom is licensed under either of:


~17K SLoC