简体   繁体   English

Memory Node.js 应用程序的使用量在写入文件时增加

[英]Memory usage of Node.js application increases when writing files

I'm connected to a p2p network and receiving messages every some seconds.我连接到 p2p 网络并每隔几秒钟接收一次消息。 The messages can have a size of 10 chars but also 5 MB.消息的大小可以是 10 个字符,但也可以是 5 MB。 I need to log all messages I receive by writing them to the file system.我需要通过将收到的所有消息写入文件系统来记录它们。 At the moment I'm using fs.writeFileSync() but unfortunately, the memory increases the longer my application is running:目前我正在使用 fs.writeFileSync() 但不幸的是,memory 增加了我的应用程序运行的时间:

In this case, my application is running for 15 hours and uses already 3.7Gi.在这种情况下,我的应用程序运行了 15 个小时并且已经使用了 3.7Gi。 The function I use for writing my logs is the following:我用来写日志的 function 如下:

let counter = 0

class Message {
    constructor(data) {
        this.msgId = data.readInt32LE()
        const length = data.readInt32LE()
        // this.data = JSON.parse(data.read(<length>))
    }
    
    // some other methods

    save() {
        if (!fs.existsSync(`/data/${this.msgId}`)) {        
            const data = Object.assign(this.data, {
              index: counter++
            })
            console.log(counter)
            fs.writeFileSync(`/data/${this.msgId}`, JSON.stringify(data))
          }
    }
}

Whenever I receive a message over p2p, I'm reading the msgId and the data it contains within the constructor.每当我通过 p2p 收到消息时,我都会读取 msgId 以及它包含在构造函数中的数据。 After doing some other business logic on the "Message" object, I finally calling the save() function.在对“消息”object 做了一些其他业务逻辑之后,我终于调用了 save() function。 The "memory leak" is definitely inside the save() function because when I leave the save() function empty, my application runs for days without cosnuming more than 200MB memory. “内存泄漏”肯定在 save() function 内,因为当我将 save() function 留空时,我的应用程序运行了几天而没有超过 200MB ZCD69B4957F0198CD818D7BF3D6 So does anyone know if fs.writeFilesync() has a potential memory leak or what might cause the memory leak?那么有谁知道 fs.writeFilesync() 是否存在潜在的 memory 泄漏或可能导致 memory 泄漏的原因?

I am having the same issue while using node 15.8.0 on windows 10.我在 windows 10 上使用节点 15.8.0 时遇到了同样的问题。

I replaced fs.writeFileSync(files[i], data, { encoding: 'utf8' });我替换了 fs.writeFileSync(files[i], data, { encoding: 'utf8' }); with

let fd = 0;
try {
   fd = fs.openSync( files[i], 'w', 0o666  );  
   let writen = fs.writeSync( fd, data, 0, 'utf8' );
} catch ( e ) {
} finally {
   if ( fd )
     fs.closeSync(fd);
}

This resolved the issue with expanding heap.这解决了扩展堆的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM