<bdo id='hv6Ux'></bdo><ul id='hv6Ux'></ul>

<small id='hv6Ux'></small><noframes id='hv6Ux'>

    1. <legend id='hv6Ux'><style id='hv6Ux'><dir id='hv6Ux'><q id='hv6Ux'></q></dir></style></legend>
      <tfoot id='hv6Ux'></tfoot>
      <i id='hv6Ux'><tr id='hv6Ux'><dt id='hv6Ux'><q id='hv6Ux'><span id='hv6Ux'><b id='hv6Ux'><form id='hv6Ux'><ins id='hv6Ux'></ins><ul id='hv6Ux'></ul><sub id='hv6Ux'></sub></form><legend id='hv6Ux'></legend><bdo id='hv6Ux'><pre id='hv6Ux'><center id='hv6Ux'></center></pre></bdo></b><th id='hv6Ux'></th></span></q></dt></tr></i><div id='hv6Ux'><tfoot id='hv6Ux'></tfoot><dl id='hv6Ux'><fieldset id='hv6Ux'></fieldset></dl></div>

      1. JavaScript 闭包是如何被垃圾回收的

        时间:2023-10-12
        <legend id='t60Lw'><style id='t60Lw'><dir id='t60Lw'><q id='t60Lw'></q></dir></style></legend>
          <tbody id='t60Lw'></tbody>
          • <bdo id='t60Lw'></bdo><ul id='t60Lw'></ul>
            <i id='t60Lw'><tr id='t60Lw'><dt id='t60Lw'><q id='t60Lw'><span id='t60Lw'><b id='t60Lw'><form id='t60Lw'><ins id='t60Lw'></ins><ul id='t60Lw'></ul><sub id='t60Lw'></sub></form><legend id='t60Lw'></legend><bdo id='t60Lw'><pre id='t60Lw'><center id='t60Lw'></center></pre></bdo></b><th id='t60Lw'></th></span></q></dt></tr></i><div id='t60Lw'><tfoot id='t60Lw'></tfoot><dl id='t60Lw'><fieldset id='t60Lw'></fieldset></dl></div>
          • <tfoot id='t60Lw'></tfoot>

          • <small id='t60Lw'></small><noframes id='t60Lw'>

                  本文介绍了JavaScript 闭包是如何被垃圾回收的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我记录了以下 .

                  我希望使用最少的内存得到一个包含 500 个 function() {} 的数组.

                  不幸的是,事实并非如此.每个空函数都持有一个(永远无法访问,但不是 GC'ed)一百万个数字的数组.

                  Chrome 最终停止并死掉,Firefox 在使用了近 4GB 的 RAM 后完成了整个过程,而 IE 逐渐变慢,直到它显示内存不足".

                  删除任一注释行可以解决所有问题.

                  似乎所有这三种浏览器(Chrome、Firefox 和 IE)都按上下文而不是按闭包保留环境记录.Boris 假设这个决定背后的原因是性能,这似乎是可能的,尽管我不确定根据上述实验可以调用它的性能如何.

                  如果需要一个引用 some 的闭包(当然我在这里没有使用它,但想象一下我用过),如果不是

                  函数 g() { 一些;}

                  我用

                  var g = (function(some) { return function() { some; }; )(some);

                  它将通过将闭包移动到与我的其他函数不同的上下文来解决内存问题.

                  这会让我的生活更加乏味.

                  附:出于好奇,我在 Java 中尝试了这个(利用它在函数内部定义类的能力).GC 就像我最初希望 Javascript 一样工作.

                  I've logged the following Chrome bug, which has led to many serious and non-obvious memory leaks in my code:

                  (These results use Chrome Dev Tools' memory profiler, which runs the GC, and then takes a heap snapshot of everything not garbaged collected.)

                  In the code below, the someClass instance is garbage collected (good):

                  var someClass = function() {};
                  
                  function f() {
                    var some = new someClass();
                    return function() {};
                  }
                  
                  window.f_ = f();
                  

                  But it won't be garbage collected in this case (bad):

                  var someClass = function() {};
                  
                  function f() {
                    var some = new someClass();
                    function unreachable() { some; }
                    return function() {};
                  }
                  
                  window.f_ = f();
                  

                  And the corresponding screenshot:

                  It seems that a closure (in this case, function() {}) keeps all objects "alive" if the object is referenced by any other closure in the same context, whether or not if that closure itself is even reachable.

                  My question is about garbage collection of closure in other browsers (IE 9+ and Firefox). I am quite familiar with webkit's tools, such as the JavaScript heap profiler, but I know little of other browsers' tools, so I haven't been able to test this.

                  In which of these three cases will IE9+ and Firefox garbage collect the someClass instance?

                  解决方案

                  I tested this in IE9+ and Firefox.

                  function f() {
                    var some = [];
                    while(some.length < 1e6) {
                      some.push(some.length);
                    }
                    function g() { some; } //removing this fixes a massive memory leak
                    return function() {};   //or removing this
                  }
                  
                  var a = [];
                  var interval = setInterval(function() {
                    var len = a.push(f());
                    if(len >= 500) {
                      clearInterval(interval);
                    }
                  }, 10);
                  

                  Live site here.

                  I hoped to wind up with an array of 500 function() {}'s, using minimal memory.

                  Unfortunately, that was not the case. Each empty function holds on to an (forever unreachable, but not GC'ed) array of a million numbers.

                  Chrome eventually halts and dies, Firefox finishes the whole thing after using nearly 4GB of RAM, and IE grows asymptotically slower until it shows "Out of memory".

                  Removing either one of the commented lines fixes everything.

                  It seems that all three of these browsers (Chrome, Firefox, and IE) keep an environment record per context, not per closure. Boris hypothesizes the reason behind this decision is performance, and that seems likely, though I'm not sure how performant it can be called in light of the above experiment.

                  If a need a closure referencing some (granted I didn't use it here, but imagine I did), if instead of

                  function g() { some; }
                  

                  I use

                  var g = (function(some) { return function() { some; }; )(some);
                  

                  it will fix the memory problems by moving the closure to a different context than my other function.

                  This will make my life much more tedious.

                  P.S. Out of curiousity, I tried this in Java (using its ability to define classes inside of functions). GC works as I had originally hoped for Javascript.

                  这篇关于JavaScript 闭包是如何被垃圾回收的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:关于闭包、LexicalEnvironment 和 GC 下一篇:如何请求 node.js 中的垃圾收集器运行?

                  相关文章

                  最新文章

                    <bdo id='wOlfo'></bdo><ul id='wOlfo'></ul>
                  <i id='wOlfo'><tr id='wOlfo'><dt id='wOlfo'><q id='wOlfo'><span id='wOlfo'><b id='wOlfo'><form id='wOlfo'><ins id='wOlfo'></ins><ul id='wOlfo'></ul><sub id='wOlfo'></sub></form><legend id='wOlfo'></legend><bdo id='wOlfo'><pre id='wOlfo'><center id='wOlfo'></center></pre></bdo></b><th id='wOlfo'></th></span></q></dt></tr></i><div id='wOlfo'><tfoot id='wOlfo'></tfoot><dl id='wOlfo'><fieldset id='wOlfo'></fieldset></dl></div>
                • <small id='wOlfo'></small><noframes id='wOlfo'>

                      <tfoot id='wOlfo'></tfoot>
                      <legend id='wOlfo'><style id='wOlfo'><dir id='wOlfo'><q id='wOlfo'></q></dir></style></legend>