Skip to content

[Bug]: transformFromAstSync leaks memory #13970

Closed

Description

@JLHwung

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

for (let i = 0; i < 1e7; i++) {
    ast = babel.transformFromAstSync(ast, src, {
      configFile: false,
      ast: true,
    }).ast;
  }

For a complete reproduction, see https://github.com/JLHwung/babel-transform-from-ast-leak

Configuration file name

No response

Configuration

N.A.

Current and expected behavior

Current: The memory usage rises beyond 2GB

Expected: The memory usage should be stable during repetitive transformFromAstSync calls.

Environment

System:
OS: macOS 12.0.1
Binaries:
Node: 17.0.1 - /opt/homebrew/bin/node
Yarn: 1.22.17 - /opt/homebrew/bin/yarn
npm: 8.1.0 - /opt/homebrew/bin/npm
npmPackages:
@babel/core: ^7.16.0 => 7.16.0

Possible solution

No response

Additional context

Found this issue when running jest --detect-leaks in the Babel codebase.

transformFromAstAsync and transformFromAst are also affected.

When switching transformFromAstSync to transformSync, the memory leakage can not be reproduced.

Activity

  1. babel-bot commented on Nov 16, 2021

    @babel-bot
    Collaborator

    Hey @JLHwung! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

    If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

  2. nicolo-ribaudo commented on Nov 16, 2021

    @nicolo-ribaudo
    Member

    Does it leak if you pass cloneAST: false?

  3. JLHwung commented on Nov 16, 2021

    @JLHwung
    ContributorAuthor

    Does it leak if you pass cloneAST: false?

    Nice catch! The leak is gone when closeInputAst is disabled. However this option is enabled by default.

    It is likely that after cloneDeep is performed, the new AST object still contains reference to the old AST object which prevents them from being collected.

  4. added
    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue
    on Sep 20, 2022
  5. locked as resolved and limited conversation to collaborators on Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions