Long Connections with Node.Js, How to Reduce Memory Usage and Prevent Memory Leak? Also Related with V8 and Webkit-Devtools

MongoDB Realm with nodejs memory leak issue

Cloning the object creates memory leak moreover opening and closing the connection multiple time creates additional overhead to the memory and creates RealmObject in the background.

Sample Image

Node and RxJs: How can I avoid a memory leak on a long process?

You could wrap your 'trash' in a another Observable and make use of Disposable to clean up, like this:

.flatMap((x) => {

return Rx.Observable.create(obs => {
let onlyTrash = _.range(1000000);

let disposable = Rx.Disposable.create(() => {
onlyTrash = undefined; // free up reference
});

obs.onNext(x);
obs.onCompleted();

return disposable;
});

});

Make sure you change it to flatMap rather than just map

Node.js memory usage remains high even after destroying sockets

Ok it seems I found the solution. Nodejs caches the Socket objects to be used in future. Here is the updated code.

server code:

'use strict';

const net = require('net');

let sockets = [];

let numberOfSockets = 2000;
let server = net.createServer(socket => {
sockets.push(socket);
if (sockets.length >= numberOfSockets) {
console.log(process.memoryUsage());
console.log('cleanup start');

for (let socket of sockets) {
socket.end();
socket.destroy();
socket.unref();
}
sockets = [];
global.gc();
setTimeout(() => {
console.log(process.memoryUsage());
}, 1000);

console.log('cleaned up and ready');
}
});

if (global.gc) {
setInterval(() => {
global.gc();
}, 15000);
}

server.listen(6000, '127.0.0.1');

client code:

'use strict';

const net = require('net');

let numberOfClosedSockets = 0;
let numberOfSockets = 2000;

function test() {
for (let i = 0; i < numberOfSockets; ++i) {
let socket = new net.Socket();
socket.connect(6000, '127.0.0.1', () => {
console.log('Connected');
});

socket.on('data', data => {
console.log(data);
});

socket.on('close', () => {
console.log('Connection closed ' + numberOfClosedSockets);
numberOfClosedSockets++;
if (numberOfClosedSockets >= numberOfSockets) {
numberOfClosedSockets = 0;
setTimeout(() => {
test();
}, 2000);

}
socket.destroy();
socket.unref();
});
}
}
test();

In this example the client creates new sets of sockets after the first sets of sockets has been destroyed. And it does it over and over again. If you look at memory usage it doesn't increase so there is no memory leak.

github issue link

Chrome DevTools, Memory: what is `feedback_cell` and how to resolve memory leak that traces to it?

Got the same question, but this article helped me to understand what is feedback_cell

https://rohitwhocodes.wordpress.com/2020/08/20/feedback-vectors-in-heap-snapshots/

TLDR;

  • feedback_cell is not a cause of memory leak
  • feedback_cell (also known as feedback vector) is array of metadata information maintained by v8 engine to optimize execution performance.

How do feedback vectors affect memory investigations?

They are a red herring and if you see a feedback_cell in the retainer
list that you are investigating, you can safely assume this specific
retainer list is not the cause of the leak.

You can think of feedback vectors are soft references and hence it is
safe to ignore them as a cause of a memory leak.

Update: as Sebastian said in comments

  • If you think that you don't have a memory leak, but you still see these feedback_cells, this means you do have a memory, however it is not caused by these references


Related Topics



Leave a reply



Submit