{"id":24719201,"url":"https://github.com/ziadasem/gc-cpp","last_synced_at":"2025-03-22T11:41:58.673Z","repository":{"id":274104282,"uuid":"921912582","full_name":"ziadasem/gc-cpp","owner":"ziadasem","description":"A different implementation for garbage collection system in C++","archived":false,"fork":false,"pushed_at":"2025-01-31T22:16:44.000Z","size":48,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-31T23:20:42.571Z","etag":null,"topics":["cpp","cpp17","garbage-collection","garbage-collector","modern-cpp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ziadasem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-24T21:20:42.000Z","updated_at":"2025-01-31T22:16:47.000Z","dependencies_parsed_at":"2025-01-31T23:19:53.903Z","dependency_job_id":null,"html_url":"https://github.com/ziadasem/gc-cpp","commit_stats":null,"previous_names":["ziadasem/gc-cpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fgc-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fgc-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fgc-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fgc-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziadasem","download_url":"https://codeload.github.com/ziadasem/gc-cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244952554,"owners_count":20537467,"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","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":["cpp","cpp17","garbage-collection","garbage-collector","modern-cpp"],"created_at":"2025-01-27T11:16:58.913Z","updated_at":"2025-03-22T11:41:58.667Z","avatar_url":"https://github.com/ziadasem.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Garbage collection systems implemention in C++\n## Overview\n\nGarbage collection refers to the process of automatically reclaiming memory that is no longer in use, preventing memory leaks and ensuring efficient memory utilization. Unlike languages like Java or Python, which have built-in garbage collection, C++ provides manual control over memory management through features like dynamic memory allocation and deallocation. However, custom garbage collection systems can be implemented in C++ to facilitate memory management.\n\nThis document explains garbage collection in C++ and provides an outline for implementing a mark and sweep garbage collection in C++.\n\n## Techniques for Garbage Collection\n\nSeveral techniques can be used to implement garbage collection in C++:\n\n### 1. Reference Counting\n\nReference counting involves keeping track of the number of references (or pointers) to an object. When the reference count drops to zero, the object can safely be deleted. This technique is typically implemented using smart pointers, such as std::shared_ptr and std::unique_ptr, in the C++ Standard Template Library (STL).\n\n\u003e Note: This repository does not implement the reference counting technique explicitly, as it is already supported through smart pointers in the STL.\n\n**Advantages**:\n\n* Simple to implement.\n* Immediate reclamation of memory.\n\n**Disadvantages**:\n\n* Cannot handle circular references.\n\n\nExample:\n```cpp\nclass RefCountedObject {\n    int refCount = 0;\n\npublic:\n    void addRef() { ++refCount; }\n    void release() {\n        if (--refCount == 0) {\n            delete this;\n        }\n    }\n};\n```\n\n### 2. Tracing garbage collection\n\nIn computer programming, tracing garbage collection is a form of automatic memory management that consists of determining which objects should be deallocated (\"garbage collected\") by tracing which objects are reachable by a chain of references from certain \"root\" objects, and considering the rest as \"garbage\" and collecting them. Tracing is the most common type of garbage collection – so much so that \"garbage collection\" often refers to the tracing method, rather than others such as reference counting – and there are a large number of algorithms used in implementation. [wikipedia]\n\nthis system can be implemented by two algorithms which are:\n#### 1. Mark and Sweep:\n\nMark-and-sweep is a two-phase garbage collection approach:\n* **Mark Phase**: Traverse all accessible objects and mark them as reachable.\n* **Sweep Phase**: Scan memory and deallocate objects that were not marked.\n\nAdvantages:\n\n* Handles circular references.\n\nDisadvantages:\n* Requires traversal of all objects, which can be time-consuming.\n* Needs additional memory for bookkeeping.\n\n#### 2. Tri-color marking:\nas can be conculded, mark-and-sweep doesn't exhibit the optimum performance, hence most modern tracing garbage collectors implement some variant of the **tri-color marking abstraction**, but simple collectors (such as the mark-and-sweep collector) often do not make this abstraction explicit. \n\nThis abstraction divides objects into three sets:\n* **White Set**: Objects that are candidates for garbage collection.\n* **Gray Set**: Objects that are reachable but whose children have not been fully explored.\n* **Black Set**: Objects that are reachable and whose children have been fully explored.\n\n**Advantages**:\n\n* Can be performed \"on-the-fly\" without halting the system for significant periods.\n* Improves efficiency compared to simpler methods like mark-and-sweep.\n\n**Disadvantages**:\n\n* More complex to implement.\n* Requires careful handling of concurrency and object state.\n\n\u003e Note: Simple collectors like mark-and-sweep often do not explicitly implement the tri-color abstraction.\n\n\n## What is in This Repository and How to Contribute\nThis repository implements a tracing garbage collector system in C++. Each implementation resides in its own directory, along with its respective documentation (a video explanation can also be created). While the current implementation provides a foundation for understanding garbage collection, there are still some gaps (or \"cavities\") in the functionality, which are explicitly declared in its corresponding documentation.\n\nContribution Guidelines:\n\n1. **Improve the Implementation**: Address existing gaps and refine the garbage collector to make it more robust and efficient.\n\n2. **Add Test Cases**: Create additional test cases to ensure the garbage collector works as expected in various scenarios.\n\nWe welcome all contributions to this repository and encourage developers to share their improvements!\n\n\u003eNote: This documentation is primarily generated by AI and fine-tuned prompts.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziadasem%2Fgc-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziadasem%2Fgc-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziadasem%2Fgc-cpp/lists"}