Skip to content

__commonJS runtime helper retains factory reference after initialization, preventing GC in long-lived processes #9063

@hyf0-agent

Description

@hyf0-agent

Description

The __commonJS runtime helper retains a reference to the factory function (cb) after the module has been initialized. In long-lived Node.js processes (e.g. SSR servers), the executed factory can never be garbage collected, causing memory to grow monotonically.

Context

When loading a Rolldown-built CJS bundle via vm.createContext + vm.runInContext (a common SSR pattern), module exports are cached in memory for request reuse. Because the __commonJS closure keeps holding the factory reference even after execution, the factory cannot be GC'd. This means each time a new version of the bundle is loaded, memory only increases and never drops back.

Reproduction

A runnable demo is available at:
https://stackblitz.com/edit/vitejs-vite-pn1msfza?file=rolldown-issue.md

Run node --expose-gc demo.mjs to reproduce.

Expected Behavior

After the module factory has been executed and exports cached, the factory reference should be released (e.g. set cb = null) so it becomes eligible for garbage collection.

Suggested Fix

In the __commonJS helper, null out the cb reference after the factory has been called:

// After calling the factory and caching the result:
cb = null;

This is a one-line fix but has significant impact for SSR and other long-lived process use cases where bundles are loaded/unloaded over time.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions