npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leak-detector

v0.0.2

Published

A memory/resource leak detector for Node.js applications

Downloads

814

Readme

leak-detector

A memory/resource leak detector for Node.js applications.

Usage

The leak-detector tool can be used to find resource leaks within your Node.js application. Run your application, make HTTP requests while using a load balancer, and keep an eye on the results. Note that there will be some performance overhead so it may be safest to test in staging or while observing production for short periods of time.

Basic Usage

Here's a mostly code-free way to enable the leak detector:

npm install leak-detector
DISABLE_LD_MEMORY=true node -r leak-detector/all myapp.js

By default all of the leak detectors will be enabled when used in this manner.

Details about memory usage will be printed to STDERR.

Advanced Usage

Here's how to enable it programmatically:

const detector = require('leak-detector');

const detectors = {
  interval: 10, // 10s interval by default

  // specify which detectors to enable
  requests: true,
};

const handler = (detector, leaks) => {
  console.error(`LEAKS DETECTED (${detector}): `, leaks);
};

if (process.env.NODE_ENV === 'staging') {
  detector.start(detectors, handler);
}

Detectors

Here's a list of detectors supported by this tool.

The "config" column specifies the detectors flag name used for enabling the detector. When using this tool programmatically all of the detectors are disabled by default unless manually enabled.

The "environment variable" column specifies the name of the environment variable used to disable the detector. When using this by injecting via command line all of the detectors are enabled by default.

| Detector | Config | Environment Variable | |-------------------|------------|-----------------------| | Incoming Requests | requests | DISABLE_LD_REQUESTS |

Currently we only support detection of one type of resource leak. We plan on adding more. Pull requests welcome.

Incoming Requests Leak Detection

This leak detector depends on the FinalizationRegistry feature of JavaScript. This is a feature that allows code to run when an object has been garbage collected but it comes with no guarantees that it will run when an object is collected. So far in my testing it seems pretty stable, but YMMV.

Chances are, an application is going to leak 100% of incoming requests or 0%. However, if there's something like an A/B test that runs on 33% of traffic, maybe the application would leak 33% or 67% of requests. If you have a code path that runs 1% of the time then it is likely indistinguishable from a margin of error. For testing purposes it's best to enable code paths 100% or 0% of the time to deterministically isolate a leak anyway.

Here's what the handler payload looks like:

{
  "starts": 1000, // Number of incoming requests that have been recorded
  "finishes": 999, // Number of incoming requests that have completed
  "collections": 998 // Number of garbage collected incoming requests
}

The starts and finishes values are transmitted by the http module itself and are very accurate. In this case there is a single open request that the application is still dealing with.

The collections value is reported based on garbage collection events in the FinalizationRegistry and may have a margin of error. In this case the one active request hasn't been garbage collected yet since it's still in use. Another request also hasn't been garbage collected. It could be a leak, or it could get garbage collected if you were to manually call the gc() function (presuming --enable-gc is enabled).