Skip to content

Memory leak with no stored references #2541

@appellation

Description

@appellation
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions