从内存中卸载Javascript [英] Unload a Javascript from memory

查看:67
本文介绍了从内存中卸载Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑浏览器上的Web应用程序。我可以在遍历应用程序的不同部分时根据需要动态添加所需的js文件,但是随着内存使用量的增长,我可以从当前会话的内存中卸载不必要的js内容吗?

I am thinking of a web application on browsers. I can dynamically add required js files as needed while traversing through different parts of application, but can I unload unnecessary js content from the current session's "memory" as the memory usage grows over time?

我知道可以删除负责内容的标签,但不能保证它最终会卸载并取消绑定与该内容相对应的所有内容。

I know it is possible to remove the tag responsible for the content but it is not assured that it will eventually unload and unbind everything correspondent to that content.

谢谢

推荐答案


我知道可以删除负责内容的标签,但它不是保证它最终将卸载和取消绑定与该内容相关的所有内容。

I know it is possible to remove the tag responsible for the content but it is not assured that it will eventually unload and unbind everything correspondent to that content.

事实上,它确保不会即可。加载并执行JavaScript后,它与加载它的脚本元素之间没有链接。

In fact, it's assured that it will not. Once the JavaScript is loaded and executed, there is no link between it and the script element that loaded it at all.

您可以动态加载和卸载模块,但必须通过

You can dynamically load and unload modules, but you have to design it into your code by


  • 将模块设计到您的代码中。让模块具有单个符号整个模块是指

  • 有一个卸载功能或类似的功能,负责删除所有模块的事件监听器等

然后卸载模块是调用其卸载函数,然后将引用它的符号设置为 undefined (或者完全删除它,如果它是对象的属性)。

Then unloading the module is a matter of calling its unload function and then setting the symbol that refers to it to undefined (or removing it entirely, if it's a property on an object).

这是一个非常简单的概念演示(其中如果你使用某种异步模块定义库,你会适应:

Here's a very simple demonstration of concept (which you'd adapt if you were using some kind of Asynchronous Module Definition library):

// An app-wide object containing a property for each module.
// Each module can redefine it in this way without disturbing other modules.
var AppModules = AppModules || {};

// The module adds itself
AppModules.ThisModule = (function() {
    var unloadCallbacks = [];

    function doThis() {
        // Perhaps it involves setting up an event handler
        var element = document.querySelector(/*...*/);
        element.addEventHandler("click", handler, false);
        unloadCallbacks.push(function() {
            element.removeEventHandler("click", handler, false);
            element = handler = undefined;
        });

        function handler(e) {
            // ...
        }
    }

    function doThat() { 
        // ...
    }

    function unload() {
        // Handle unloading any handlers, etc.
        var callbacks = unloadCallbacks;
        unloadCallbacks = undefined;
        callbacks.forEach(function(callback) {
            callback();
        });

        // Remove this module
        delete AppModules.ThisModule;
    }

    return {
        doThis: doThis,
        unload: unload
    };
})();

回调机制非常粗糙,你想要更好的东西。但它证明了这个概念。

That callbacks mechanism is very crude, you'd want something better. But it demonstrates the concept.

这篇关于从内存中卸载Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆