I have a web page that starts a HTML5 SharedWorker
script. Chrome memory usage increases every time this page is reloaded (hitting F5).
The worker script is very simple. Every second (using setInterval
) a message is sent to the connected port.
It seems like the worker process is terminated and restarted each time I hit F5. This is what I'd expect since the worker isn't actually shared by more than one 'document'. However, I cannot figure out why memory usage increases on every refresh.
Does anybody know why this is happening?
Given that memory increases each time the page is reloaded makes me think that I cannot use shared workers at all in Chrome. Has anyone been able to do so without having memory problems?
UPDATE
This is the hosting HTML:
<div id="output"></div>
<script type="text/javascript" src="/scripts/jquery-1.4.4.js"></script>
<script type="text/javascript">
$(function () {
var worker = new SharedWorker("/scripts/worker.js")
worker.port.onmessage = function(e) {
$("#output").append($("<div></div>").text(e.data));
};
worker.port.start();
});
</script>
...and this is worker.js
:
var list = [];
setInterval(function () {
for (var i = 0; i < list.length; ++i) {
list[i].postMessage("#connections = " + list.length);
}
}, 1000);
onconnect = function (event) {
list.push(event.ports[0]);
};
The hosting page starts/connects to a shared worker and outputs whatever is received from it.
The worker code keeps a list of connected ports and sends a message to them all once a second.
This is simple stuff. Yet, each time the hosting page is reloaded in Chrome. Memory payload for that tab is increased.
The following shows Chrome's memory usage after a couple of refreshes:
...after refreshing some more I'm reaching 250 MB...
I'm running out of ideas, thinking this must be a bug in Chrome. Can anyone give me some sort of pointer?
UPDATE 2
Disabling my AdBlock extension seemed to fix the problem:
So I was happy for a little while but it turned out that memory is still being leaked. With AdBlock disabled it's just leaking quite a bit less per page refresh.
Visualize memory consumption with the performance profiler The performance profiler in Chrome can visualize memory usage and graph it over time. To try this out, open the DevTools in Chrome and switch to the Performance tab. Note that we use an Incognito window when measuring performance.
Memory leaks are a common error in programming, especially when using languages that have no built in automatic garbage collection, such as C and C++. Typically, a memory leak occurs because dynamically allocated memory has become unreachable.
Start with metrics such as page load times, HTTP request times, and Core Web Vitals – time to the first byte, first contentful paint. If you use Sematext Experience you'll see a number of other useful metrics for your web applications and websites there. However, metrics themselves are only a part of the whole picture.
The SharedWorker interface represents a specific kind of worker that can be accessed from several browsing contexts, such as several windows, iframes or even workers. They implement an interface different than dedicated workers and have a different global scope, SharedWorkerGlobalScope .
Seems like the Chromium-team have resolved this problem. I cannot reproduce it anymore.
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