Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible memory leak when JSON.parse throws #35048

Closed
kemarsh opened this issue Sep 4, 2020 · 2 comments
Closed

Possible memory leak when JSON.parse throws #35048

kemarsh opened this issue Sep 4, 2020 · 2 comments
Labels
memory Issues and PRs related to the memory management or memory footprint.

Comments

@kemarsh
Copy link

kemarsh commented Sep 4, 2020

Possible recurrence of #33266

In some product code we've seen a steady memory leak from one of our modules. I've narrowed the cause down to calls to JSON.parse() where the string provided is invalid json and causes the function to throw an Error.

Below, I've provided a simple recreate which when I run on the Chrome debugger, puts about 18 MB on the heap which doesn't seem to ever get GCed. It sounds much like the issue already reported in #33266 but this I understand was resolved in node 12.18.2.

  • Version: 12.18.3, 14.9.0 (NOT recreatable on 10.18.1)
  • Platform: Windows 10 (64-bit)
  • Subsystem:

What steps will reproduce the bug?

Run the following code - ideally under a debugger to allow inspection of the heap.

'use strict'

function test () {
  for (let i = 0; i < 100000; i++) {
    try {
      JSON.parse('not valid json')
    } catch (e) {
      // Do nothing
    }
  }
}

test()
console.log('done')

In the heapdump I see thousands of these entries all retained by Global handles

image

How often does it reproduce? Is there a required condition?

It will happen every time you run the sample code provided

What is the expected behavior?

Garbage does not accumulate on the heap

What do you see instead?

Additional information

@lpinca lpinca added the memory Issues and PRs related to the memory management or memory footprint. label Sep 4, 2020
@mmarchini
Copy link
Contributor

mmarchini commented Sep 4, 2020

That sounds surprisingly similar to #33266, including the contents on the snapshot. If you let the code run indefinitely, does it crash for lack of space? Is it reproducible on previous v12 versions as well? Does it also happen on other platforms or is it happening only on Windows?

@kemarsh
Copy link
Author

kemarsh commented Sep 4, 2020

So here's a graph of the rss for different node versions over a much larger number of iterations than my original recreate.

image

Looks like only v12.15.0 is showing growth with later versions showing flat rss.

I'd conclude from that that my original suspicion that 14.9.0 was showing the problem was in fact incorrect and this issue has been resolved.

Closing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
memory Issues and PRs related to the memory management or memory footprint.
Projects
None yet
Development

No branches or pull requests

3 participants