久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<legend id='t60Lw'><style id='t60Lw'><dir id='t60Lw'><q id='t60Lw'></q></dir></style></legend>
      • <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 class="qwawimqqmiuu" id='t60Lw'><tfoot id='t60Lw'></tfoot><dl id='t60Lw'><fieldset id='t60Lw'></fieldset></dl></div>
    1. <tfoot id='t60Lw'></tfoot>

    2. <small id='t60Lw'></small><noframes id='t60Lw'>

        JavaScript 閉包是如何被垃圾回收的

        How JavaScript closures are garbage collected(JavaScript 閉包是如何被垃圾回收的)

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

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

              <legend id='hv6Ux'><style id='hv6Ux'><dir id='hv6Ux'><q id='hv6Ux'></q></dir></style></legend>
              <tfoot id='hv6Ux'></tfoot>
                <tbody id='hv6Ux'></tbody>
              <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 class="qwawimqqmiuu" id='hv6Ux'><tfoot id='hv6Ux'></tfoot><dl id='hv6Ux'><fieldset id='hv6Ux'></fieldset></dl></div>

                1. 本文介紹了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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                    <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 class="qwawimqqmiuu" id='wOlfo'><tfoot id='wOlfo'></tfoot><dl id='wOlfo'><fieldset id='wOlfo'></fieldset></dl></div>
                  • <small id='wOlfo'></small><noframes id='wOlfo'>

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

                          • 主站蜘蛛池模板: 97伦理最新伦理 | 国产99久久精品 | 在线观看av不卡 | 蜜桃传媒av | 国产一级片在线观看视频 | 国产精品国产成人国产三级 | 91影院 | 国产成人免费视频网站视频社区 | 91在线影院 | 久久高清免费视频 | 日韩国产一区二区三区 | 久久久成人网 | 日韩国产一区二区三区 | 一区2区 | 午夜一区二区三区在线观看 | 亚洲精品视频一区 | www.日韩系列 | 538在线精品| 久久99精品国产 | 午夜性色a√在线视频观看9 | 成人国产一区二区三区精品麻豆 | www.婷婷 | 在线看片网站 | 日韩精品久久久久 | 久久r久久| 中文字幕一级 | 91色在线| 久久精品欧美一区二区三区不卡 | 伦理午夜电影免费观看 | 毛片区 | 久久精品91 | 成人午夜影院 | 美美女高清毛片视频免费观看 | 日韩av电影院 | 国产伦精品一区二区三区照片91 | 秋霞国产 | 中文字幕在线一区二区三区 | 激情国产视频 | 亚洲国产精品99久久久久久久久 | 成人免费视频久久 | 懂色一区二区三区免费观看 |