https://github.com/quark-zju/gcmodule
Garbage collection for Rust inspired by CPython's gcmodule
https://github.com/quark-zju/gcmodule
garbage-collection rust
Last synced: about 1 month ago
JSON representation
Garbage collection for Rust inspired by CPython's gcmodule
- Host: GitHub
- URL: https://github.com/quark-zju/gcmodule
- Owner: quark-zju
- License: mit
- Created: 2020-03-02T02:58:41.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-05T10:40:14.000Z (almost 3 years ago)
- Last Synced: 2025-04-04T02:41:26.912Z (about 2 months ago)
- Topics: garbage-collection, rust
- Language: Rust
- Homepage:
- Size: 147 KB
- Stars: 21
- Watchers: 2
- Forks: 6
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gcmodule
[](https://docs.rs/gcmodule)
[](https://crates.io/crates/gcmodule)
Garbage collection inspired by [CPython](https://github.com/python/cpython/)'s implementation.
This library provides a type `Cc`. It provides shared reference-counting pointer, similar to stdlib `Rc`. Unlike `Rc`, reference cycles in `Cc` can be collected.
If all values can be freed by just reference-counting, the collector used by this library does not take extra memory. This is different from some other implementations, which require manual collection to free the extra memory used by the collector.
## Example
```rust
use gcmodule::{Cc, Trace};
use std::cell::RefCell;type List = Cc>>>;
let a: List = Default::default();
let b: List = Default::default();
a.borrow_mut().push(Box::new(b.clone()));
b.borrow_mut().push(Box::new(a.clone())); // Form a cycle.
drop(a);
drop(b); // Internal values are not dropped due to the cycle.
gcmodule::collect_thread_cycles(); // Internal values are dropped.
```For customized structures, they need to implement the `Trace` interface. That can be done by `#[derive(Trace)]`.
```rust
use gcmodule::{Cc, Trace};
use std::cell::RefCell;#[derive(Trace, Default)]
struct List(RefCell>>);
{
let a: List = Default::default();
let b: List = Default::default();
a.borrow_mut().push(Box::new(b.clone()));
b.borrow_mut().push(Box::new(a.clone()));
}
assert_eq!(gcmodule::collect_thread_cycles(), 2); // 2 values are collected.
```Refer to [the documentation](https://docs.rs/gcmodule/) for more examples and technical details.
## Similar Projects
### [bacon-rajan-cc](https://github.com/fitzgen/bacon-rajan-cc) v0.3
- Both are reference counted, with cyclic garbage collection.
- Both are mainly single-threaded, and stop-the-world.
- Main APIs like `Cc` and `Trace` are similar, or even compatible.
- `gcmodule` is conceptually simpler. There is no need for the "colors" concept.
- `gcmodule` provides `ThreadedCc` for multi-thread environment.
- `bacon-rajan-cc` requires manual collection to release GC metadata (but not the tracked object) even if the reference count logically drops to 0. See [this commit message](https://github.com/quark-zju/gcmodule/commit/b825bc45ac008e26ada3c13daa3efa34334f8273) for some details.### [rcgc](https://github.com/jonas-schievink/rcgc) v0.1
- `rcgc` takes a novel approach - the collector holds strong references while everywhere else uses weak references.
- Therefore, `rcgc` requires manual collection to release actual objects even if the reference count of objects (logically) drops to 0.