Question

In my web app I constantly have to re-instantiate Worker objects, because there is no way of passing new script to old existing objects. After a while, some browsers start blocking creation of new Worker objects, because their limits of Workers for one window is reached. I tried to delete finished worker objects before creating new ones, but apparently I do it in a wrong way. Here is a simple test, which fails in opera (limit is 256 workers per window):

var worker;    
for(var i = 0; i < 300; i++) {
  for (var key in worker) worker[key] = null;
  delete worker.onmessage;
  delete worker.onerror;
  delete worker;
  worker = new Worker("script.js");
}

My question would be how to delete a worker object completely, so that browser limit would never be reached? Thank you!

Was it helpful?

Solution

Not sure if this will work with web worker. Do you create the object on window? If so you can try this:

delete window.worker

Maybe call terminate and then delete it?

window.worker.terminate();
delete window.worker;

The documentation states that it'll stop everything but doesn't clean up when terminate() is called.

OTHER TIPS

you need to terminate the worker, not delete the object. All deleting the object does is remove the pointer to the worker. In my experience having too many OPEN workers is a problem that will prevent your program from working but there is no limit to workers that have been closed either by self.close(); in the worker or worker.terminate(); in the main script. You should never have to use delete worker ever if you are closing your workers correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top