{"id":18498374,"url":"https://github.com/softdevteam/libgc","last_synced_at":"2025-08-01T08:34:45.398Z","repository":{"id":52593782,"uuid":"248285041","full_name":"softdevteam/libgc","owner":"softdevteam","description":"A library for garbage collection in Rust.","archived":false,"fork":false,"pushed_at":"2021-04-23T15:28:25.000Z","size":126,"stargazers_count":13,"open_issues_count":4,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T18:01:06.397Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/softdevteam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-18T16:30:20.000Z","updated_at":"2025-03-22T10:10:08.000Z","dependencies_parsed_at":"2022-09-07T05:23:23.271Z","dependency_job_id":null,"html_url":"https://github.com/softdevteam/libgc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/softdevteam/libgc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Flibgc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Flibgc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Flibgc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Flibgc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/softdevteam","download_url":"https://codeload.github.com/softdevteam/libgc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softdevteam%2Flibgc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268192592,"owners_count":24210541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T13:39:17.069Z","updated_at":"2025-08-01T08:34:45.336Z","avatar_url":"https://github.com/softdevteam.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libgc\n\n[![Bors enabled](https://bors.tech/images/badge_small.svg)](https://bors.tech)\n\n_libgc_ is a garbage collection library for Rust. It can be used as a standalone\nlibrary, but it is highly recommended that programs are compiled with the\ncompanion [rustc fork](https://github.com/softdevteam/rustgc), which offers\nlanguage support for much better performance.\n\n_libgc_ is in active development - there will be bugs!\n\n## Example\n\n_libgc_ provides a smart pointer, `Gc\u003cT\u003e`, which can be used to make garbage\ncollected values. An example, with the necessary global allocator setup, looks\nas follows:\n\n```rust\nuse libgc::{Gc, GcAllocator};\n\n#[global_allocator]\nstatic ALLOCATOR: GcAllocator = GcAllocator;\n\n\nfn foo() -\u003e Gc\u003cVec\u003cusize\u003e\u003e {\n    let foo = Gc::new(vec![1,2,3]);  \n    let a = foo; // GC pointers are copyable\n    let b = foo;\n\n    foo \n}\n\nfn main() {\n    let gc = foo();\n}\n```\n\n## Overview\n\nIf you want to write code with shared ownership in Rust, `Rc` makes this\npossible. Unfortunately, managing cyclic data structures with reference counting\nis hard: weak pointers are needed to break strong cycles and thus prevent memory\nleaks. In programs where these sorts of structures are common,\ngarbage collection is a natural fit.\n\n_libgc_ is not a replacement to the single ownership model - it is intended to\ncomplement it by providing a garbage collected alternative for values which\nmight be too difficult to manage with `Rc`. Values must opt-in to using\ngarbage collection with the `Gc::new(x)` constructor. This tells _libgc_ to heap\nallocate `x`, and GC it for you when you're done with it. `Gc` can be thought of\nas a special `Box` type, where `x`'s lifetime is unknown at compile-time.\nPeriodically, the garbage collector will interrupt the program (known as\n\"stopping the world\") to see which `Gc` values are still in use, and drop those\nwhich aren't. \n\nGarbage collection involves scanning parts of the stack and heap to look for\nlive references to `Gc` values. This means that _libgc_ must be aware of all heap\nallocated values, even those which aren't `Gc`. To do this, _libgc_ has its own\nallocator, `GcAllocator`, which must be set as the global allocator when using\n_libgc_.\n\n```rust\nuse libgc::GcAllocator;\n\n#[global_allocator]\nstatic ALLOCATOR: GcAllocator = GcAllocator;\n```\n\n### Finalization\n\nA `Gc` can be used to manage values which have a `drop` method. Like all tracing\ngarbage collectors, _libgc_ can not provide any guarantees about exactly when a\n'dead' value is dropped. Instead, once _libgc_ has determined that a value is\nunreachable, its `drop` method is added to a drop queue, which is ran on a\nparallel finalization thread at some point in the future. The order of\nfinalization is intentionally undefined to allow _libgc_ to run `drop` methods on\nvalues which contain cycles of `Gc`.\n\n:warning: You must not dereference a field of type `Gc\u003cT\u003e` inside `Drop::drop`.\nDoing so is unsound and can lead to dangling pointers. TODO: Add a lint for this\nand explain why in further details.\n\n## Implementation\n\n__libgc__ is implemented using the [Boehm-Demers-Weiser\ncollector](https://github.com/https://github.com/ivmai/bdwgc).  It is a\nconservative, stop-the-world, parallel, mark-sweep collector.\n\nTODO: Expand\n\n## Known Issues\n\n* Single-threaded support only.\n* No Drop Lint to prevent unsound dereferencing of `Gc` typed fields.\n\n## Using libgc with rustgc\n\nThere are two repositories which make up the gc infrastructure:\n\n* **libgc** the main library which provides the `Gc\u003cT\u003e` smart pointer and its\n      API.\n* **rustgc** a fork of rustc with GC-aware optimisations. This can be used to\n      compile user programs which use libgc, giving them better GC\n      performance. Use of rustgc is not mandated, but it enables further\n      optimisations for programs which use libgc.\n\nThis seperation between libgc and rustgc exists so that a stripped-down form of\ngarbage collection can be used without compiler support. \n\nTODO: Explain rustgc and it's optimizations.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftdevteam%2Flibgc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftdevteam%2Flibgc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftdevteam%2Flibgc/lists"}