为什么在Linux上使用Node.js时内存持续增加?

huangapple go评论74阅读模式
英文:

Why memory keeps increasing while using Nodejs on Linux?

问题

以下是您提供的代码的中文翻译部分:

async function saveIntoMp4(chunks) {
    const options = {
        type: "video/webm"
    }
    let blob = new Blob(chunks, options);
    chunks.length = 0 // 为了防止内存泄漏
    const buffer = Buffer.from(await blob.arrayBuffer());
    try {
        fs.writeFile(
            `./videos/1.mp4`,
            buffer,
            () => console.log("视频已保存!")
        );
    } catch (error) {
        console.log(error)
    }
}

关于内存问题,您提到每次调用该函数都会增加内存使用量,达到了4GB。根据您的描述,我认为这可能是一个内存泄漏的问题。您可以尝试以下方式来解决:

  1. 在函数的开头,使用 console.log 来输出 chunks 的长度,以确保它在函数内被正确清空。

  2. 检查其他地方是否有可能导致内存泄漏,例如全局变量的持续增长或事件监听器没有正确移除。

  3. 确保您的Node.js版本和相关依赖库是最新的,因为一些问题可能已在后续版本中修复。

  4. 尝试使用内存分析工具,如Node.js的heapdump或Chrome DevTools,来进一步识别内存泄漏的原因。

希望这些提示可以帮助您找到并解决内存泄漏问题。

英文:

I have a little piece of code as such :

async function saveIntoMp4(chunks) {
    const options = {
        type: "video/webm"
    }
    let blob = new Blob(chunks, options);
    chunks.length = 0 // to stop any memory leaks
    const buffer = Buffer.from(await blob.arrayBuffer());
    try {
        fs.writeFile(
            `./videos/1.mp4`,
            buffer,
            () => console.log("video is saved!")
        );
    } catch (error) {
        console.log(error)
    }
}

And everytime this function is called the occupied memory of mine goes up. It reached 4gb in fact. I have tracked the heap usage with node --inspect and I realized that arrayBuffer is infact emptied out without an issue. Yet the memory that started at 75mb reached more than 100mb just by calling the function once as is shown in the picture. Is this a memory leak? How can I stop this from happening. I have tested it on Ubuntu 20 / 22 / Centos 9 and Rocky linux ( red hat variant ) all the same situation. The only place I am not facing memory issues is my windows 11. :

为什么在Linux上使用Node.js时内存持续增加?

答案1

得分: 0

这个问题困扰我已经10天了,我认为这与内存分配器有关,根据 https://github.com/Unitech/pm2/issues/4375

我在Ubuntu 20上通过 apt-get 安装了 libjemalloc

并使用以下命令进行检查:

echo "/usr/lib/x86_64-linux-gnu/libjemalloc.so.2" >>
/etc/ld.so.preload

然后通过以下命令进行了检查:

ps aux | grep node cat /proc//smaps | grep jemalloc

问题似乎已经解决了。

英文:

Okay so this had been bothering me for 10 days, I believe this had something to do with the memory allocator according to https://github.com/Unitech/pm2/issues/4375

I installed libjemalloc through apt-get on Ubuntu 20 by

> echo "/usr/lib/x86_64-linux-gnu/libjemalloc.so.2" >>
> /etc/ld.so.preload

and checked using

> ps aux | grep node cat /proc/<PID>/smaps | grep jemalloc

The problem seems to be fixed.

huangapple
  • 本文由 发表于 2023年6月26日 19:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定