Git Product home page Git Product logo

gcmodule's Introduction

gcmodule

Documentation crates.io Build Status

Garbage collection inspired by CPython's implementation.

This library provides a type Cc<T>. It provides shared reference-counting pointer, similar to stdlib Rc<T>. 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

use gcmodule::{Cc, Trace};
use std::cell::RefCell;

type List = Cc<RefCell<Vec<Box<dyn Trace>>>>;
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)].

use gcmodule::{Cc, Trace};
use std::cell::RefCell;

#[derive(Trace, Default)]
struct List(RefCell<Vec<Box<dyn Trace>>>);
{
    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 for more examples and technical details.

Similar Projects

  • Both are reference counted, with cyclic garbage collection.
  • Both are mainly single-threaded, and stop-the-world.
  • Main APIs like Cc<T> and Trace are similar, or even compatible.
  • gcmodule is conceptually simpler. There is no need for the "colors" concept.
  • gcmodule provides ThreadedCc<T> 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 for some details.

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.

gcmodule's People

Contributors

quark-zju avatar mio-19 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.