locked
javascript worker memory leak

    Question

  • Hi,

    I wrote a project to test the worker by running a loop to post a message, it works. However, the memory is keeping increasing.

    Here is my code:

    default.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>worker</title>
    
        <!-- WinJS references -->
        <link href="//Microsoft.WinJS.0.6/css/ui-dark.css" rel="stylesheet">
        <script src="//Microsoft.WinJS.0.6/js/base.js"></script>
        <script src="//Microsoft.WinJS.0.6/js/ui.js"></script>
    
        <!-- worker references -->
        <link href="/css/default.css" rel="stylesheet">
        <script src="/js/default.js"></script>
    </head>
    <body style="overflow: scroll;">
        <button id="post">post</button>
        <button id="cancelpost">cancel post</button>
        <p id="output"></p>
    </body>
    </html>
    

    default.js

    // For an introduction to the Blank template, see the following documentation:
    // http://go.microsoft.com/fwlink/?LinkId=232509
    (function () {
        "use strict";
    
        var app = WinJS.Application;
    
        var id = function (ele) {
            return document.getElementById(ele);
        };
    
        app.postMessageToWorker = function (eventInfo) {
    
            worker.addEventListener('message', app.processMessageFromWorker, false);
            worker.postMessage(eventInfo);
        };
    
        app.processMessageFromWorker = function (event) {
            var data = event.data;
            switch (data.type) {
                case 'optimizeAdjust':
                    break;
                case 'applyEffect':
                    var p = document.createElement('p');
                    p.innerHTML = data.d;
                    id('output').appendChild(p);
                    break;
                default:
                    break;
            }
        }
        var worker = new Worker("/js/worker.js");
        app.onactivated = function (eventObject) {
            if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
                if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) {
                    // TODO: This application has been newly launched. Initialize
    
                    var interval = null;
                    id('post').onclick = function () {
                        var eventData = {
                            type: "applyEffect",
                            d: 'This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive.'
                        };
                        interval = setInterval(function () {
                            app.postMessageToWorker(eventData);
                            window.CollectGarbage();
                        }, 500);
                    };
    
                    id('cancelpost').onclick = function () {
                        clearInterval(interval);
                    };
    
    
    
    
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
                }
                WinJS.UI.processAll();
            }
        };
    
        app.oncheckpoint = function (eventObject) {
            // TODO: This application is about to be suspended. Save any state
            // that needs to persist across suspensions here. You might use the
            // WinJS.Application.sessionState object, which is automatically
            // saved and restored across suspension. If you need to complete an
            // asynchronous operation before your application is suspended, call
            // eventObject.setPromise().
        };
    
        app.start();
    })();
    

    worker.js

    self.addEventListener('message', function (event) {
        var data = event.data;
        switch (data.type) {
            case 'optimizeAdjust':
                self.postMessage(data);
                break;
            case 'applyEffect':
                self.postMessage(data);
                break;
            default:
                break;
        }
    }, false);


    sun

    Thursday, April 19, 2012 7:05 AM

Answers

  • You are adding an object to the DOM with every message.  It makes sense that the memory would increase right?

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, April 19, 2012 1:56 PM
    Moderator