Abfall - Concurrent Tri-Color Tracing Garbage Collector
A concurrent mark-and-sweep garbage collector library for Rust using the tri-color marking algorithm.
Features
- Tri-Color Marking: Uses white, gray, and black colors to track object reachability
- Concurrent Collection: Background thread performs garbage collection automatically
- Thread-Safe: Safe to use across multiple threads
- Manual Control: Option to disable automatic collection and trigger manually
Architecture
Tri-Color Algorithm
The garbage collector uses a tri-color marking scheme:
- White: Potentially unreachable objects (candidates for collection)
- Gray: Reachable objects that haven't been scanned yet
- Black: Reachable objects that have been fully scanned
Mark and Sweep Phases
-
Mark Phase: Starting from root objects, the GC marks all reachable objects by:
- Coloring all roots gray
- Processing gray objects: mark as black and add references to gray queue
- Continue until no gray objects remain
-
Sweep Phase: Reclaim memory from white (unmarked) objects
Usage
Basic Example
use ;
use Arc;
// Create a new GC context with automatic background collection
let ctx = new;
// Allocate objects on the GC heap
let value1 = ctx.allocate;
let value2 = ctx.allocate;
let value3 = ctx.allocate;
// Access values through smart pointers
println!;
println!;
println!;
// When pointers go out of scope, objects become unreachable
// and will be collected in the next GC cycle
Manual Collection
use GcContext;
use Duration;
// Create context without background collection
let ctx = with_options;
let ptr = ctx.allocate;
drop; // Object is now unreachable
// Manually trigger collection
ctx.collect;
Concurrent Usage
use GcContext;
use Arc;
use thread;
let ctx = new;
let mut handles = vec!;
for i in 0..10
for handle in handles
License
This project is licensed under either of
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the project by you shall be dual licensed as above, without additional terms or conditions.