iOS JavaScript Workers High CPU After Terminate()

iOS Javascript Workers High CPU after terminate()

the easiest way to terminate the worker (and be sure of it) would be to inject a boolean expression to cause the code to "exception out" of its main loop (or even return gracefully if there is only 1 or 2 bottlenecks in the code.). and you can perform any required cleanup in a catch block prior to letting it exit naturally.

I personally hate ending threads with functions such as terminate or kill because it can leave things in a undefined state

The call to terminate may get ignored or enqueued for later simply because the worker is using all the resources it can leaving no time for message processing.

dispatch_async UIWebView loadrequest

Ok guys, I created a Worker in my Javascript code, so I can handle it like new threads but not in iOS (which was impossible to do using UIWebView). The solution is here: iOS Javascript Workers High CPU after terminate()

Web Worker consumes massive amount of memory

I found that if I run the garbage collector manually from developer tools -> Timeline it clears out all of the memory. Similarly, if I begin interacting with the Worker context from the console, calling functions seems to randomly trigger successful gc.

Based on this, I would say that there is not a hanging reference, but that receiving objects via a transfer may not force a gc check as new allocation requests would.

Transferring the object back with a response seems to workaround the problem:

postMessage('hi', [ev.data]);  // process usage stays around 50MB

As an alternative, making sure the Worker is non-trivial and will need to do normal allocations also seems to properly trigger gc, i.e:

  postMessage('hi');
var twoMB = new ArrayBuffer(8388608); // usage cycles 70MB - ~220MB

How can I prevent the backgroundWorker causing the UI to become sluggish?

You could use threadpriority:

Thread.CurrentThread.Priority = ThreadPriority.Highest;

This is however considered to be poor form in most cases since the operating system is in a better position to decide what program deserves CPU time. And it does not need to follow your request for more time, even if you explicitly ask for it.

If plotting takes a considerable amount of time you might consider:

  • Can you optimize the plotting somehow?
  • Can you reduce the number of points?
    • you could perhaps plot a smaller part of the dataset?
    • You could pre-process the plot to reduce the point density. Screens typically have a resolution of 2k-4k, so if you have a line-chart with more points the user will not be able to see it anyway.


Related Topics



Leave a reply



Submit