https://github.com/codemix/garbage-collector
A garbage collector for JavaScript built on top of typed arrays.
https://github.com/codemix/garbage-collector
Last synced: 8 months ago
JSON representation
A garbage collector for JavaScript built on top of typed arrays.
- Host: GitHub
- URL: https://github.com/codemix/garbage-collector
- Owner: codemix
- License: mit
- Created: 2016-02-07T19:30:58.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-20T22:22:06.000Z (almost 10 years ago)
- Last Synced: 2024-10-06T03:19:30.426Z (over 1 year ago)
- Language: JavaScript
- Size: 18.6 KB
- Stars: 8
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# garbage-collector
A garbage collector built on top of typed arrays.
[](https://travis-ci.org/codemix/garbage-collector)
## What?
A simple reference counting Garbage Collector built for use with [malloc](https://github.com/codemix/malloc).
## Installation
Install via [npm](https://npmjs.org/package/garbage-collector).
```sh
npm install garbage-collector
```
## Usage
```js
import Allocator from "malloc";
import GarbageCollector from "garbage-collector";
const heap = new Buffer(1024 * 1024);
const allocator = new Allocator(heap); // heap could also be an ArrayBuffer
const gc = new GarbageCollector(allocator, {
// The number of cycles before an item with no references will be freed.
lifetime: 2,
// The callbacks which will be invoked when a tagged block is freed.
// Keys must be integers within uint32 range, greater than zero.
callbacks: {
1: (offset) => {
console.log('Freeing string at', offset);
}
}
});
console.log(gc.inspect());
const input = "Hello World";
const offset = gc.alloc(Buffer.byteLength(input), 1); // 1 is the type tag, it's optional.
heap.write(input, offset);
gc.ref(offset); // increment the reference count
gc.cycle(); // our data is preserved because it has a reference count > 0
console.log(gc.inspect());
console.log(gc.sizeOf(offset));
gc.unref(offset); // decrement the reference count by 1
const freed = gc.cycle(); // frees our string and invokes the callback
console.log('freed', freed, 'bytes');
```
## License
Published by [codemix](http://codemix.com/) under a permissive MIT License, see [LICENSE.md](./LICENSE.md).