0

I'm connected to a p2p network and receiving messages every some seconds. The messages can have a size of 10 chars but also 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:

In this case, my application is running for 15 hours and uses already 3.7Gi. The function I use for writing my logs is the following:

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. After doing some other business logic on the "Message" object, I finally calling the 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. So does anyone know if fs.writeFilesync() has a potential memory leak or what might cause the memory leak?

2
  • Something is fishy here - are you actually passing data, which is an object, to fs.writeFileSync? Commented Mar 29, 2021 at 11:38
  • Yeah, sorry... there's another function (I removed to shrink down the excerpt) that is called before the save() function. It's doing some business logic and finally does this.data = JSON.stringify(this.data). I fixed the missing JSON.stringify() above.... Commented Mar 29, 2021 at 14:24

1 Answer 1

1

I am having the same issue while using node 15.8.0 on windows 10.

I replaced 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, this reduced the memory usage dramatically. So I can write 1 Mio. messages which was impossible previously.
Hi, could you explain what's the cause of the issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.