New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

garbage-collector

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

garbage-collector

A simple garbage collector built on top of typed arrays.

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

garbage-collector

A garbage collector built on top of typed arrays.

Build Status

What?

A simple reference counting Garbage Collector built for use with malloc.

Installation

Install via npm.

npm install garbage-collector

Usage

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 under a permissive MIT License, see LICENSE.md.

Keywords

garbage collector

FAQs

Package last updated on 02 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts