What I'm planning to do is the following:
1. Start a Worker.
2a. This worker must load some huge data (via AJAX or importScripts
), do something with it and get the result which will be stored in a variable in this worker.
2b. Load new portion of data (which must replace the previous portion), do something with it and append the desired result to the variable.
2c. ...and so on.
3. The memory usage will grow, so I must be able to force the browser to close this worker and "forget" about it. I will lose all my data, but get rid of a memory loss.
4. I will start this worker again, when I want.
I've made this test in portable Chrome-based browser (with --allow-file-access-from-files
flag enabled, otherwise Worker won't start):
app.html:
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<script src="jquery.js"></script>
<script>
$(function () {
$('#click').on("click", function () {
var w = new Worker('worker.js');
w.onmessage = function (event) {
console.log(event.data);
};
w.postMessage({
"url": "hugedata1.js"
});
$('#click2').on("click", function () {
w.postMessage({
"url": "hugedata2.js"
});
});
$('#terminate').on("click", function () {
w.terminate();
});
});
});
</script>
</head>
<body>
<div id="click">start worker</div>
<div id="click2">load second portion of data</div>
<div id="terminate">terminate worker</div>
</body>
</html>
worker.js:
var hugedataarr;
var myarr = [];
self.onmessage = function(event) {
importScripts(event.data.url);
myarr[myarr.length] = hugedataarr[0];
self.postMessage(myarr);
}
hugedata.js:
hugedataarr = [
{ "id": "1", "A": "b"},
//imagine millions of objects placed in here
{ "id": "5000000", "F": "g"}
]
hugedata2.js:
hugedataarr = [
{ "id": "5000001", "H": "i"},
//imagine millions of objects placed in here
{ "id": "10000000", "X": "y"}
]
Make sure that hugedata files are huge enough so the memory loss will be visible immediately.
Steps: 1) open "app.html" 2) click "start worker", wait, check console log 3) click "load second portion of data", wait, check console log 4) click "terminate worker" or refresh the page 5) check the memory loss 6) repeat steps 2-4 to increase the memory loss.
Question: how do I really close this worker, so it gives back its memory? Or, if it's not possible, how to really refresh the application page so the browser "forgets" about that worker once and for all and releases occupied memory? (because pressing F5 to refresh the page won't do anything to solve the memory problem)