2

I am noticing that in both Google Chrome and Microsoft Edge the Worker objects are not garbage collected. After running the following standalone simple test case, doing memory profile and checking for Worker class shows all the worker objects as retained.

<html>
        <body>
                <div id="app"></div>
                <script>
                        const js = 'var array = new Array(1024*1024).fill("Hello world")';
                        const blob = new Blob([js], { type: 'application/javascript' });
                        const wurl = URL.createObjectURL(blob);
                        for(let i=1;i<=100;i++) {
                                const w = new Worker(wurl);
                                const result = { id: i };
                                document.getElementById('app').innerHTML = JSON.stringify(result);
                                w.terminate();
                        }
                </script>
        </body> 
</html>

How to ensure the worker objects are freed?

smcjones
  • 5,490
  • 1
  • 23
  • 39
Siva
  • 1,096
  • 7
  • 20
  • Are the worker objects just not yet freed (because garbage collection didn't run yet), or are they actually retained by anything? – Bergi Mar 22 '22 at 07:37
  • The memory profiler is showing that all other objects are freed and only Worker objects are retained. I have isolated it all the way to the above code which clearly shows that there is nothing that is holding on to the workers. – Siva Mar 22 '22 at 08:27
  • I don't know if the worker binds anything around the url, but in case I would try calling `revokeObjectURL` afterward. – Jean-Baptiste Martin Apr 01 '22 at 08:11
  • I just tried and it didn't work. – Siva Apr 01 '22 at 09:19
  • This question tickled me and after some tests and researches I can't find a valid reason why the workers are retained. This situation is mentioned many times on stackoverflow but no real solution is given – Jean-Baptiste Martin Apr 02 '22 at 13:13

1 Answers1

-2

Off the top of my head I'd say that your problem lies with the const w = new Worker... part. I'm guessing the garbage cleanup isn't taking because the value of w cannot be reassigned, and in any case block cleanup is irrelevant because it's a detached process to begin with. Try changing it to var w = new Worker..., leaving the rest unchanged.

Yrth
  • 188
  • 1
  • 4
  • 1
    Declaring a variable as `const` does not affect garbage collection. – Bergi Apr 01 '22 at 15:26
  • Changing it to `var` didn't help either. – Siva Apr 01 '22 at 15:46
  • 1
    That's not how const and var work at all – Jean-Baptiste Martin Apr 02 '22 at 11:30
  • 1
    `var` would retain the last w worker on the root scope while `const` would limit w to the for loop scope. That being said I don't see how this changes anything for op issue. – Jean-Baptiste Martin Apr 02 '22 at 13:10
  • To look at the code, there is nothing obviously wrong with it. Except it doesn't work. It leaks. From previous comments, this scenario has been posted several times, but no solutions yet. So you've got three possibilities. A) The language is bugged. B) There is information missing. C) there are effecs at play that aren't obvious. The idea I pose is an easy ask. Change five letters to three letters. If it works, then try to figure out why. If it doesn't - change it back and no harm done. "That's dumb!" is not useful either way. – Yrth Apr 04 '22 at 13:05