Worker objects are not freed

Viewed 151

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?

1 Answers

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.

Related