DEV Community

Srijan Karki
Srijan Karki

Posted on

Node.js Timeouts and Memory Leaks

Memory leaks on nodejs- Memory Leak Issue: Node.js timeouts can easily create memory leaks.

  • Timeout API: Unlike browsers, where setTimeout returns a number, Node.js returns a Timeout object.
  • Timeout Object: This object includes several properties and can retain references, causing memory issues.

Example of Memory Leak

class MyThing {
  constructor() {
    this.timeout = setTimeout(() => { ... }, INTERVAL);
  }

  clearTimeout() {
    clearTimeout(this.timeout);
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Problem: The Timeout object remains even after clearTimeout or timeout completion, holding references that prevent garbage collection.

Impact of AsyncLocalStorage

  • AsyncLocalStorage: Attaches state to timeouts, making leaks worse.
  • Example:
const { AsyncLocalStorage } = require('node:async_hooks');
const als = new AsyncLocalStorage();

let t;
als.run([...Array(10000)], () => {
  t = setTimeout(() => {
    const theArray = als.getStore();
  }, 100);
});
Enter fullscreen mode Exit fullscreen mode
  • Result: Timeout holds a reference to a large array, exacerbating the memory issue.

Suggested Fix

  • Use ID Instead of Object:
  class MyThing {
    constructor() {
      this.timeout = +setTimeout(() => { ... }, INTERVAL);
    }

    clearTimeout() {
      clearTimeout(this.timeout);
    }
  }
Enter fullscreen mode Exit fullscreen mode
  • Problem: Current Node.js bug makes this approach cause an unrecoverable memory leak.

Workaround

  • Aggressive Nullification:
  class MyThing {
    constructor() {
      this.timeout = setTimeout(() => {
        this.timeout = null;
      }, INTERVAL);
    }

    clearTimeout() {
      if (this.timeout) {
        clearTimeout(this.timeout);
        this.timeout = null;
      }
    }
  }
Enter fullscreen mode Exit fullscreen mode

Broader Implications

  • Widespread Use: Many applications use timeouts and intervals, leading to potential memory issues.
  • Next.js: Patches setTimeout and setInterval to clear intervals, though it can encounter related bugs.

Long-Term Considerations

  • Better API Design: Node.js could improve by returning a lightweight proxy object instead of the full Timeout object.
  • AsyncLocalStorage Management: Need for APIs to prevent propagation of unnecessary state to avoid leaks.

Conclusion

  • Memory Management: Developers need to be cautious with timeouts and AsyncLocalStorage to avoid memory leaks.
  • Awaiting Fix: A fix for the underlying Node.js bug is crucial for long-term resolution.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay