{"id":20963861,"url":"https://github.com/codemix/garbage-collector","last_synced_at":"2025-05-14T09:31:50.984Z","repository":{"id":57244426,"uuid":"51262164","full_name":"codemix/garbage-collector","owner":"codemix","description":"A garbage collector for JavaScript built on top of typed arrays.","archived":false,"fork":false,"pushed_at":"2016-03-20T22:22:06.000Z","size":19,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-06T03:19:30.426Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/codemix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-07T19:30:58.000Z","updated_at":"2023-03-04T10:36:01.000Z","dependencies_parsed_at":"2022-09-01T04:30:31.269Z","dependency_job_id":null,"html_url":"https://github.com/codemix/garbage-collector","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fgarbage-collector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fgarbage-collector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fgarbage-collector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fgarbage-collector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/garbage-collector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225284590,"owners_count":17449932,"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-19T02:48:39.992Z","updated_at":"2024-11-19T02:48:40.659Z","avatar_url":"https://github.com/codemix.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# garbage-collector\nA garbage collector built on top of typed arrays.\n\n[![Build Status](https://travis-ci.org/codemix/garbage-collector.svg?branch=master)](https://travis-ci.org/codemix/garbage-collector)\n\n## What?\nA simple reference counting Garbage Collector built for use with [malloc](https://github.com/codemix/malloc).\n\n## Installation\n\nInstall via [npm](https://npmjs.org/package/garbage-collector).\n```sh\nnpm install garbage-collector\n```\n\n## Usage\n\n```js\nimport Allocator from \"malloc\";\nimport GarbageCollector from \"garbage-collector\";\n\nconst heap = new Buffer(1024 * 1024);\nconst allocator = new Allocator(heap); // heap could also be an ArrayBuffer\n\nconst gc = new GarbageCollector(allocator, {\n  // The number of cycles before an item with no references will be freed.\n  lifetime: 2,\n  // The callbacks which will be invoked when a tagged block is freed.\n  // Keys must be integers within uint32 range, greater than zero.\n  callbacks: {\n    1: (offset) =\u003e {\n      console.log('Freeing string at', offset);\n    }\n  }\n\n});\n\nconsole.log(gc.inspect());\n\nconst input = \"Hello World\";\nconst offset = gc.alloc(Buffer.byteLength(input), 1); // 1 is the type tag, it's optional.\nheap.write(input, offset);\n\ngc.ref(offset); // increment the reference count\n\ngc.cycle(); // our data is preserved because it has a reference count \u003e 0\n\nconsole.log(gc.inspect());\n\nconsole.log(gc.sizeOf(offset));\n\ngc.unref(offset); // decrement the reference count by 1\n\nconst freed = gc.cycle(); // frees our string and invokes the callback\n\nconsole.log('freed', freed, 'bytes');\n```\n\n\n## License\n\nPublished by [codemix](http://codemix.com/) under a permissive MIT License, see [LICENSE.md](./LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fgarbage-collector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Fgarbage-collector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fgarbage-collector/lists"}