2

I'm using a webworker to pass some data at an interval of 10 ms. In the task manager I can see the working memory set increasing till I don't cancel the interval.

Here's what I'm doing:

Sending:

function send() {
 setInterval(function() { 
    const data = {
     array1: get100Arrays(),
     array2: get500Arrays()
    };

    let json = JSON.stringify( data );
    let arbfr = str2ab (json);
    worker.postMessage(arbfr, [arbfr]);
  }, 10);
}


function str2ab(str) {
   var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
   var bufView = new Uint16Array(buf);
   for (var i=0, strLen=str.length; i<strLen; i++) {
     bufView[i] = str.charCodeAt(i);
   }
   return buf;
 }

I also tried to do only this, but with no success:

// let json =  JSON.stringify( data );
// let arbfr = str2ab(json);
worker.postMessage(data);

Anyone know why this might be leaking? I'm currently trying this on Chrome.

roro
  • 193
  • 3
  • 16

1 Answers1

1

Usually memory leaks in web workers are created while passing values multiple times between the main thread and the worker thread.
If possible try to send the array to the web worker only once.
You can also detect if your Transferable Objects work properly (The array will be neutered)

var ab = new ArrayBuffer(1);

try {
   worker.postMessage(ab, [ab]);

   if (ab.byteLength) {
      console.log('TRANSFERABLE OBJECTS are not supported in your browser!');
   } 
   else {
     console.log('USING TRANSFERABLE OBJECTS');
   }
} 
catch(e) {
  console.log('TRANSFERABLE OBJECTS are not supported in your browser!');
}
Sagi
  • 8,972
  • 3
  • 33
  • 41
  • Due to the nature of work, it has to be sent very frequently. And sending only once makes it kinda hard to see the memory change. – roro Oct 19 '15 at 21:30
  • 1
    Maybe your transferable objects are not working correctly. try to return the bufView instead of the buf and call worker.postMessage(bufView.buffer, [bufView.buffer]) – Sagi Oct 19 '15 at 21:47
  • I tried using the solution you posted to check if transferable objects work properly and also tried using bufView instead of buf. Both of these didn't stop the memory increase. – roro Oct 20 '15 at 14:30
  • Maybe you allocate memory in a pace which is faster than the pace of the objects disposable of the garbage collection, I suggest that you raise the number of milliseconds for the setInterval and check whether the garbage collector can manage to the release some memory. – Sagi Oct 20 '15 at 18:00
  • Doesn't seem like that. I changed it to 5000 ms and same thing. Mem usage doesn't go down at all. – roro Oct 20 '15 at 18:05
  • Did you try to change your get500Arrays to 500 empty arrays and check the mem? Maybe you can chunkify the size of the arrays? – Sagi Oct 20 '15 at 18:34
  • I changed it to 1 empty array, that doesn't help. Also tried doing postMessage(null) – roro Oct 20 '15 at 18:44
  • what is your chrome version? – Sagi Oct 20 '15 at 19:25
  • Version 45.0.2454.101 m – roro Oct 20 '15 at 19:32
  • Can you post a public page with your code? maybe on jsfiddle.net? – Sagi Oct 20 '15 at 19:33
  • Unfortunately not, there's a lot of it, sorry. I just put the very basic concept here. – roro Oct 20 '15 at 19:38