Within my project, there exists an angular factory responsible for managing the websocket connection with a c++ application.
The structure of this websocket factory is as follows:
.factory('SocketFactory', function ($rootScope) {
var factory = {};
factory.connect = function () {
if(factory.ws) { return; }
var ws = new WebSocket('ws://...');
ws.onopen = function () {
console.log('Connection to the C++ App established');
};
ws.onerror = function () {
console.log('Failed to establish the connection to the C++ App');
};
ws.onclose = function () {
console.log('Connection to the C++ App closed');
};
ws.onmessage = function (message) {
//perform operations here
};
factory.ws = ws;
};
factory.send = function (msg) {
if(!factory.ws){ return; }
if(factory.ws.readyState === 1) {
factory.ws.send(JSON.stringify(msg));
}
};
return factory;
});
The c++ application will transmit images via websockets, which will then be displayed in a canvas that updates with each new image received.
Although everything functions correctly, upon sending images to the browser, I noticed an increase in memory usage by the Chrome process in Ubuntu's system resource monitor, fluctuating around +/-5mb each time ws.onMessage
is triggered (approximately).
Even after removing the code within ws.onMessage
and solely retaining event detection, the memory consumption continues to rise. Should I utilize $destroy
to resolve this issue and prevent such memory leaks?