{"id":19237995,"url":"https://github.com/audulus/collector","last_synced_at":"2025-04-21T06:31:40.194Z","repository":{"id":24953449,"uuid":"28371246","full_name":"audulus/collector","owner":"audulus","description":"A concurrent garbage collector for C++","archived":false,"fork":false,"pushed_at":"2015-07-01T05:30:03.000Z","size":168,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T13:58:05.363Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/audulus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-23T02:02:05.000Z","updated_at":"2023-09-23T19:58:16.000Z","dependencies_parsed_at":"2022-08-22T20:50:51.188Z","dependency_job_id":null,"html_url":"https://github.com/audulus/collector","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audulus%2Fcollector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audulus%2Fcollector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audulus%2Fcollector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/audulus%2Fcollector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/audulus","download_url":"https://codeload.github.com/audulus/collector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250008206,"owners_count":21359948,"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":[],"created_at":"2024-11-09T16:28:58.138Z","updated_at":"2025-04-21T06:31:39.982Z","avatar_url":"https://github.com/audulus.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"collector\n=========\n\nA concurrent garbage collector for C++. Call it periodically from a background thread to sweep up your junk.\n\n### Advangates\n\n* Unlike reference counting, handles cycles.\n* Usable in a real-time thread, because collection can occur in another thread. (Just need to collect often enough, see below)\n* Supports multiple mutator threads. (\"Mutator\" threads are just your threads that aren't the collector thread.)\n* Coexists peacefully with other forms of C++ memory management.\n* Offers the same conncurrency guarantees as `shared_ptr` (I think, hah)\n* Battle-tested in a real app (I haven't attributed any bugs to the collector, but I make no guarantees!)\n* \u003c 500 lines and dirt simple.\n\n### Disadvantages\n\n* The author is a noob. He's just a graphics programmer with no real experience with GC.\n* Intrusive. You need to derive from `Collectable`. Has to be intrusive so virtual destructors can be called from the collector.\n* Requires a little discipline with use of `RootPtr` and `EdgePtr` smart pointers.\n* Pointer assignment is probably super slow because it puts an event on a queue (I haven't profiled it).\n* Currently, if you don't collect often enough, a fixed-size queue will fill up and you'll block.\n* Collection isn't optimized.\n* Your code can block if you don't collect often enough.\n\n### How to Use\n\nYou'll need [Boost](http://www.boost.org) and C++11. Drop `Collector.cpp` and `Collector.hpp` in your project. \n\nLet's say you're doing a graph data structure. You might have something like this:\n\n```c++\nclass Node : public Collectable {\n  \n public:\n  \n  // Pass a pointer to a Node via a RootPtr, since it's on the stack.\n  void AddEdge(const RootPtr\u003cNode\u003e \u0026node) {\n  \n    // Store pointers in the heap as EdgePtrs.\n    _edges.push_back( EdgePtr\u003cNode\u003e(this, node) );\n  }\n  \n private:\n\n  std::vector\u003c EdgePtr\u003c Node \u003e \u003e _edges;\n};\n```\n\n**The basic idea is whenever you are pointing to a `Collectable` on the stack, use a `RootPtr`. For pointers living in the heap, use an `EdgePtr`**. (Maybe they should be called `StackPtr` and `HeapPtr`. I just use terms from the GC research papers I can't understand.)\n\nTo create a collected object in the example, we'd do:\n\n```c++\nRootPtr\u003cNode\u003e node(new Node); // The collector uses the normal new and delete allocators.\n```\n\nThere are two constructors for `EdgePtr`:\n\n* `EdgePtr\u003cT\u003e::EdgePtr(Collectable* owner)` Creates a NULL `EdgePtr` with an owner.\n* `EdgePtr\u003cT\u003e::EdgePtr(Collectable* owner, const RootPtr\u003cT\u003e\u0026 p)` Creates an `EdgePtr` from `owner` to `p`.\n\nYou can shoot yourself in the foot by forgetting about the `owner`. In practice I've found it pretty easy to avoid doing so.\n\nWhen you want to collect, just call: `Collector::GetInstance().Collect()` periodically from a background thread. If you only call it from the main thread, your code can deadlock. To reduce blocking, you can call `Collector::GetInstance().ProcessEvents()` in the background thread more often than you call `Collect`. My collector thread looks like this:\n\n```c++\nint n = 0;\nwhile (go) {\n\n\t// Update frequently to keep the event\n\t// queue from filling up.\n\tCollector::GetInstance().ProcessEvents();\n\n\t// Collect occasionally.\n\tif ((++n % 100) == 0) {\n\t\tCollector::GetInstance().Collect();\n\t}\n\n\t// Sleep for .01 seconds.\n\tboost::this_thread::sleep(boost::posix_time::millisec(10));\n\n}\n```\n\nEnjoy!\n\n### Todo\n* Reduce the possibility that the mutator thread will block\n* Add some debug sanity checks for `RootPtr` and `EdgePtr`.\n* Be less of a noob.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faudulus%2Fcollector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faudulus%2Fcollector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faudulus%2Fcollector/lists"}