I have an app which calls web worker after the button click. The calculations are moved to worker to relieve UI and make it responsive to user actions while calculations are being made.
Everything goes okay and after about 0.8-1.5s the worker sends a response. In worker.onmessage
I perform all the needed DOM actions, but after this Garbage Collector appears and practically blocks the UI for 2 or more seconds depending on CPU. This is really confusing me, because UI blocking is what I want to prevent.
Here's the screenshot of timeline/memory console tab:
As you can see Garbage Collector events happen just after all DOM manipulations. Actually there's only one repaint event (DocumentFragment
is used).
Main js code:
var sortWorker = new Worker('js/contactsorter.js');
sortWorker.onmessage = function(e) {
var messages = [];
e.data.forEach(function(userDoc) {
var contactSection = _drawContact(userDoc);
messages.push(contactSection);
});
meta.append(messages); // this actually appends document fragment as a child
};
sortWorker.postMessage(postMessageData);
contactsorter.js (worker):
onmessage = function(e) {
var uid, output = [], usersStat = {};
// calculations...
postMessage(output);
close();
};
Is there any way to avoid these Garbage Collector events in this place or not?
UPD: it seems to me that Garbage Collector event(s) time depends on data amount that was sent to worker.
UPD2: After shutdown and boot Garbage Collector events happen only twice thus blocking UI for less than a second. Hm?
A memory leak is a situation where unused objects occupy unnecessary space in memory. Unused objects are typically removed by the Java Garbage Collector (GC) but in cases where objects are still being referenced, they are not eligible to be removed.
In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.
Although garbage collection prevents many types of memory leaks, it doesn't prevent all of them. In automatic reference counting systems, such as Perl or Objective-C, memory is leaked whenever there are cyclical references, since the reference count is never decremented to zero.
A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector is unable to remove them from memory, and therefore, they're unnecessarily maintained.
One thing to remember with Web Workers, and especially as you have them used in your example, is that you are cloning the object when you send it over to the worker. So lets run through this with a silly example:
Since you said that this was a chrome app (another comment), then maybe you can restructure your code to make use of Transferable Objects to avoid the object Cloning creation of temporary objects etc. Course, to use transferable objects you'd have to restructure as an array buffer and that's a dark magic all of itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With