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

Memory leak with no stored references #2541

Closed
appellation opened this issue Mar 21, 2020 · 3 comments
Closed

Memory leak with no stored references #2541

appellation opened this issue Mar 21, 2020 · 3 comments

Comments

@appellation
Copy link

appellation commented Mar 21, 2020

  • Node.js Version: 13.10.1
  • OS: Windows 10 (1903)
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant): util?

I own the lavalibs/encoding repository and I am encountering a strange memory leak that I cannot resolve. This package consists of two pure functions that de/serialize a specialized encoding, so it does not allocate any state outside of structures needed to perform the de/serialization. One of my users was investigating this problem and wrote a script (below) that I was able to use to verify the memory leak. This script consistently resulted in memory usage over 400MB.

I eliminated the base64-js package as a source for the memory leak by running only the Base64.toByteArray function. Likewise I also verified that using Buffer.alloc results in expected memory usage (multiple allocs get GC'd as expected). I also ran the script with --expose-gc and explicitly triggered GC before generating the heapdump. The heapdump shows a lot of functions and arrays in memory, but I wasn't able to determine any useful information from that.

I am confused about how this script is causing a memory leak, since all of the decoded data should be marked for GC immediately after the loop iteration. I'm hypothesizing that the memory leak is related to either the TextDecoder (used to read binary data into the decoded structure) or DataView (used to read specific slices from the input Uint8Array) potentially keeping some sort of circular reference preventing GC, but I was unable to determine anything more specific than that.

I know that investigating memory leaks and heapdumps is not a fun or exciting experience, but I'm really lost on what could be causing this issue. Thank you for any insight you can provide.

const { getHeapSnapshot } = require('v8');
const { createWriteStream } = require('fs');

const track = "QAAAkwIANFNrcmlsbGV4ICYgSGFic3RyYWt0IC0gQ2hpY2tlbiBTb3VwIFtPZmZpY2lhbCBBdWRpb10ABU9XU0xBAAAAAAADLIAACzIyTVdyV1BWX1FNAAEAK2h0dHBzOi8vd3d3LnlvdXR1YmUuY29tL3dhdGNoP3Y9MjJNV3JXUFZfUU0AB3lvdXR1YmUAAAAAAAAAAA==";

function reportMemory() {
  console.log(process.memoryUsage().heapUsed / 1024 / 1024, 'MB');
}

const { decode } = require('@lavalink/encoding');

for (let i = 0; i <= 100000; i++) {
  decode(track);
}

console.log('done');

setTimeout(() => {
  gc(); // only with --expose-gc flag
  reportMemory();
  getHeapSnapshot().pipe(createWriteStream('./heap.heapsnapshot'));
}, 1000);

Edit: note where we explicitly triggered GC

@Hakerh400
Copy link

Hakerh400 commented Mar 22, 2020

Simplified code without any external dependency:

'use strict';

const fs = require('fs');
const util = require('util');
const v8 = require('v8');

for(let i = 0; i !== 5e5; i++)
  new util.TextDecoder().decode(Buffer.from('abcde'));

setTimeout(() => {
  gc();
  console.log(process.memoryUsage().heapUsed / (1 << 20), 'MB');
  v8.getHeapSnapshot().pipe(createWriteStream('./heap.heapsnapshot'));
}, 1e3);

@Hakerh400
Copy link

This eventually crashes the process:

while(1) new util.TextDecoder().decode(Buffer.from('abcde'));

Seems like a memory leak in util.TextDecoder

@appellation
Copy link
Author

@Hakerh400 Thank you for taking a look! I honestly hadn't considered investigating Node's internals for a memory leak. It doesn't look as if anyone has reported this issue or made a fix for it, and it still occurs on Node v13.11.0. I'll open this issue on the main NodeJS repository with this additional information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants