nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

Memory leak on reload

Open suns-echoes opened this issue 11 years ago • 12 comments

I have found possible memory leak. When I execute any of those statements:

  • gui.Window.get().reloadIgnoringCache();
  • gui.Window.get().reload();
  • location.reload();

it will reload main page BUT not release resources! Each call increase number of documents, nodes and listeners. Even forcing GC will not get rid of this.

OS: Windows 7 64bit NWJS: 12.1, 12.2

err

Shouldn't those methods clean up all resources regardless of reference structure? Or should I manualy clean up things first and then reload?

suns-echoes avatar May 26 '15 20:05 suns-echoes

My mistake. I didn't clean up some references correctly and that caused the leak.

suns-echoes avatar May 26 '15 21:05 suns-echoes

@cmosespl what kind of references u cleaned up to fix the problem? I have the memory leak problem when I open and close app windows many times.

wufeng87 avatar Jul 08 '15 04:07 wufeng87

I did not realize that this kind of reload does not destroy everything (like close and reopen app). However, if I reload my app I found that it created another document that is usually not freed from memory. So after reload (does not matter how many times) I have two times the amount of original nodes in document - unless I dereference everything that starts from document object. If so, I have 2 documents and one node. And GC will not pick that up. I'm not sure if that is correct behaviour, but it's clean enought for me not to bother any more.

suns-echoes avatar Jul 08 '15 05:07 suns-echoes

@cmosespl could you give some more details about how you cleared it up please? How did you dereference the document exactly? We have the same problem in 12.3. Thanks.

srlowe avatar Oct 28 '15 11:10 srlowe

Hi.

I just wrote a function that iterates through all nodes below document element and dereference all links (attributes and child nodes). It is fired before the reload. Such approach might be an overkill in some situations, but at least I have no more old node objects in memory after reload. One exception is that I always end up having at least one old (but empty) document node - but this is not a problem since everything below was destroyed by my function.

suns-echoes avatar Oct 28 '15 11:10 suns-echoes

@cmosespl Thanks. Do you happen to still have that function handy? We've tried something like that and it had no effect for us. Would be interesting to see how you were doing it exactly.

srlowe avatar Oct 28 '15 12:10 srlowe

Well, in some apps I'm using it.

Result of my solution:

image

As you can see NWJS is holding a lot of garbage before cleaning it automatically. But if you force it to clean, then you see that almost all resources were released from memory. For me it is still a bug that old documents are not removed entirely. Also I was unable to locate and free few event listeners. But this is not a problem since they are overwritten each time you reload.

See memory usage right after initialization, then multiple reloads + dynamic nodes generation after each reload and finaly last reload + forced GC cleaning


In window main sript:

function __windowUnloadListener () {

    // REMEMBER TO ALWAYS REMOVE ALL EXISTING EVENT LISTENERS !!!
    // Not only this one!
    window.removeEventListener('unload', __windowUnloadListener);

    // Uninitialize application.
    app.uninitialize();

    blackhole(app); // <--- app is my application container object.
    //app = null;
    blackhole(document); // <--- obvious :)
    //document = null;

}

window.addEventListener('unload', __windowUnloadListener);

BlackHole function:

/**
 *
 * Author: Robert (CMoses) Kałamański
 *
 * DOM tree and JS object disintegrator.
 *
 * When DOM node is given then this function will break DOM tree into unreferenced nodes and remove all attributes.
 * If JS object is passed then it will be broken down into unreferenced elements. Also all DOM nodes referenced by
 * object will be destroyed.
 *
 */
function blackhole ( object, path ) {

    var child
      , type
      , attrs
      , keys
      , i;

    path = path || '';

    // Object must not be empty.
    if ( object ) {

        // DOM node.
        if ( object instanceof Node ) {

            if ( object instanceof HTMLDocument ) {
                path = 'DOCUMENT';
            } else {
                path += '>' + object.nodeName;
            }

            // Remove attributes.
            attrs = object.attributes;

            if ( attrs ) {

                for ( i=attrs.length-1; i>=0; i-- ) {

                    console.log('✘ DOM: ' + path + '[' + attrs[i].name + '=' + attrs[i].value + ']');

                    attrs[i] = null;

                }

            }

            // Remove childs.
            while ( (child = object.firstChild) ) {

                console.log('✘ DOM: ' + path + '>' + child.nodeName);

                blackhole(child, path);
                child.remove();

            }

        }

        // JS object.
        else if ( typeof object === 'object' ) {

            keys = Object.keys(object);

            for ( i=keys.length-1; i>=0; i-- ) {

                console.log('✘ OBJ: ' + path + '.' + keys[i]);

                // JS function.
                if ( typeof object[keys[i]] === 'function' ) {
                    object[keys[i]] = null;
                }

                // JS object.
                else if ( typeof object === 'object' ) {
                    blackhole(object[keys[i]], path + '.' + keys[i]);
                }

                object[keys[i]] = null;

            }

        }

    }

}

suns-echoes avatar Oct 28 '15 13:10 suns-echoes

@cmosespl Thanks so much! Will give it a try.

srlowe avatar Oct 28 '15 13:10 srlowe

Sure. I hope it will help.

suns-echoes avatar Oct 28 '15 13:10 suns-echoes

Hum, I've been in trouble and found this. My question is why this isn't done by node-webkit ?

I'll try this "blackhole", maybe it'll help.

Inateno avatar May 10 '16 14:05 Inateno

It seems that this problem can still be reproduced by running these functions gui.Window.get().reloadIgnoringCache(); gui.Window.get().reload(); location.reload();

Also leaving the node webkit program running for days in system tray, seems to do the same without even reloading the window. We managed to get to about 1.5gb of RAM on windows7/10, however not on all computers

drown-zed avatar Mar 09 '17 13:03 drown-zed

This issue still persists in in version 0.34.2 (win10). I have a largish game that reloads on new levels, and each reload adds an additional 800mb of memory usage. This same behavior does not happen in Chrome 70. I've attempted to use the reloadDev() method as suggested, but it doesn't appear to be a method in version 0.34.2. I've also looked into nw.Window.get().reload(3) to see if it would keep memory expanding, but it doesn't appear to do anything.

Is it possible to get more eyes on this?

Saltallica avatar Nov 11 '18 16:11 Saltallica