Skip to content

Buffer.from(str) memory leak #38300

Closed
Closed
@Daninet

Description

@Daninet
  • Version: v14.16.1, v15.14.0, v16.0.0
  • Platform: Microsoft Windows NT 10.0.19042.0 x64
  • Subsystem: Buffer

What steps will reproduce the bug?

Run the following code with node --expose-gc

const str = 'x'.repeat(10000);

console.log('start', process.memoryUsage());

for(let i = 0; i < 1e6; i++) {
  Buffer.from(str); // memory leak happens here
  if (i % 1e5 === 0) {
    gc();
    console.log('step', i, 'rss', process.memoryUsage().rss);
  }
}

gc();
console.log('end', process.memoryUsage());

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

It reproduces consistently with v14.16.1 and v15.14.0.
It only reproduces with a string parameter. Arrays are not causing issues.
I also tested some older versions (v10.22.0, v12.8.1) and the leak is not present there.

What is the expected behavior?

Created buffers should be garbage collected, and memory usage shouldn't increase too much during execution.

What do you see instead?

Memory usage increases to over 10 GB with the supplied code snippet. I get OOM errors when I increase the number of iterations.

start {
  rss: 18243584,
  heapTotal: 4055040,
  heapUsed: 3168160,
  external: 260263,
  arrayBuffers: 10778
}
step 0 rss 19763200
step 100000 rss 1078853632
step 200000 rss 2133364736
step 300000 rss 3189673984
step 400000 rss 4241575936
step 500000 rss 5293998080
step 600000 rss 6354276352
step 700000 rss 7406329856
step 800000 rss 8458436608
step 900000 rss 9510563840
end {
  rss: 10562609152,
  heapTotal: 5980160,
  heapUsed: 3170888,
  external: 21400146,
  arrayBuffers: 10054
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.memoryIssues and PRs related to the memory management or memory footprint.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions