简体   繁体   English

Javascript内存管理泄漏

[英]Javascript Memory Management Leaks

Currently I am creating a web Application. 目前,我正在创建一个Web应用程序。 Where users should be able to be running my application all day. 用户应该能够整天运行我的应用程序。 Currently I am having some memory issues. 目前,我遇到了一些内存问题。 Where the browser seems to crash. 浏览器似乎崩溃的地方。 What I was using is this kind of structure: 我正在使用的是这种结构:

function Module() {
    var _me = this;

    this.init = function(){
        _me.setBindings(); // Using reference from Module instead of this
    }

   // All kind of functions

   this.init();
}

Which I changed to this . 我改成了this

So a more complex situation is this (which is actually a part of my code atm): 因此,这是一个更复杂的情况(这实际上是我的代码atm的一部分):

$.modules.dynamic_static_webpage.prototype.addRedirect = function (anum, aeditor) {
    $.prompt(
            $.utils.getTranslation("Redirect"),
            $.utils.getTranslation("Geef de URL op waar naar toe geredirect moet worden"),
            $.proxy(function (num, editor, input) {
                this.clearRedirect(editor);
                var val = input.val();

                if (val.indexOf("www") == 0) {
                    val = "http://" + val;
                }

                // Timeout needed, because otherwise the clear is not finished
                setTimeout($.proxy(function (n, e, v) {
                    $.HTMLTexteditorField.setIframeSelectionHTML.call(e, "{CMS-REDIRECT" + n + "_" + v + "}");
                    this.redirectShow(n, v);
                }, this, num, editor, val), 200);
            }, this, anum, aeditor)
       );
};

Now I've added the $.proxy a lot. 现在,我添加了很多$.proxy Which seems to be a bit strange. 这似乎有点奇怪。

I have a lot of "using variables from outside the scope, inside the scope". 我有很多“在范围内,范围内使用变量”。 Which I rewrite to the above code. 我将其重写为上面的代码。 I've looked on different sites like this, but can't figure it out: 我曾在类似的不同站点上浏览过,但无法弄清楚:

  1. http://www.ibm.com/developerworks/web/library/wa-memleak/ http://www.ibm.com/developerworks/web/library/wa-memleak/
  2. How do JavaScript closures work? JavaScript闭包如何工作?

Can someone explain me if this is the correct approach to avoid memor leaks? 有人能解释一下这是否是避免记忆泄漏的正确方法吗? Or is there a better solution? 还是有更好的解决方案?

There's nothing in your code that would indicate to me that you'll have memory leak issues. 您的代码中没有任何东西可以向我表明您将遇到内存泄漏问题。 More concerningly, however, is that the style of code that you're using to avoid memory leaks is making it very difficult to tell whether there will be memory problems going forward. 然而,更令人担忧的是,您正在使用的避免内存泄漏的代码风格使很难判断是否会出现内存问题。 You've posted a few dozen lines of code; 您已经发布了几十行代码; a codebase with a few thousand lines of code written in that style might be impossible to properly audit for memory leaks. 一个以这种样式编写的数千行代码的代码库可能无法正确检查内存泄漏。

Some memory management techniques that you could use: 您可以使用一些内存管理技术:

  • If an object no longer needs to be used, dereference it. 如果不再需要使用对象,请取消引用。 The garbage collector will clean the object up for you. 垃圾收集器将为您清理对象。
  • If you dynamically call addEventListener , always call removeEventListener unless the DOM node that you're binding events to gets destroyed later. 如果您动态调用addEventListener ,请始终调用removeEventListener除非您要绑定事件的DOM节点稍后被销毁。
  • If you reference an object from within a function, then reference the function somewhere, you still have a reference to the object. 如果从函数中引用对象,然后在某处引用该函数,则仍然有对该对象的引用。 Avoid keeping references to things that you no longer need. 避免保留对不再需要的内容的引用。

Following those three guidelines will get you most of your way through a career dealing with JavaScript without any memory-related issues :) 遵循这三个指导原则将使您在处理JavaScript的职业生涯中大半路而无任何与内存相关的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM