引用循环如何在那些在JavaScript中实现引用计数垃圾收集的浏览器中创建内存泄漏?详细地

引用循环如何在那些在JavaScript中实现引用计数垃圾收集的浏览器中创建内存泄漏?详细地,javascript,memory-leaks,reference-counting,Javascript,Memory Leaks,Reference Counting,在阅读有关JS中内存管理的MDN文档时,我遇到了以下代码: function f(){ var o = {}; var o2 = {}; o.a = o2; // o references o2 o2.a = o; // o2 references o return "azerty"; } f(); // Two objects are created and reference one another thus creating a cycle. // They wil

在阅读有关JS中内存管理的MDN文档时,我遇到了以下代码:

function f(){
  var o = {};
  var o2 = {};
  o.a = o2; // o references o2
  o2.a = o; // o2 references o

  return "azerty";
}

f();
// Two objects are created and reference one another thus creating a cycle.
// They will not get out of the function scope after the function call, so they
// are effectively useless and could be free'd.
// However, the reference-counting algorithm considers that since each of both
// object is referenced at least once, none can be garbage-collected.
但它在内部是如何工作的,为什么会产生内存泄漏呢

以下是我的理解:

1) `f();`: function f gets called;

2) `var o = {};`: an object is instantiated (memory looks like this):
       ________
      | Object |
      |________|
           ^
           |
           |
           o (the symbol 'o' is a reference to an Object allocated in memory)


3) `var o2 = {};`: another object is instantiated, now memory looks like this: 

       ________                        ________    
      | Object |                      | Object |
      |________|                      |________|
           ^                               ^
           |                               |
           |                               |
           o                               o2


4) `o.a = o2; // o references o2` and `o2.a = o; // o2 references o`: memory now looks like this (correct me if I'm wrong):

       ________                         ________    
      | Object | <-     -------------> | Object |
      |________|   |   /               |________|
           ^        --/---------             ^
           |         /          \            |
           |        /            \           |
           o       /              \          o2
            \--> .a                .a <-----/
1)`f();`:函数f被调用;
2) `var o={};`:对象被实例化(内存如下所示):
________
|反对|
|________|
^
|
|
o(符号“o”是对内存中分配的对象的引用)
3) `var o2={};`:另一个对象被实例化,现在内存如下所示:
________                        ________    
|对象| |对象|
|________|                      |________|
^                               ^
|                               |
|                               |
氧
4) `o.a=o2;///o参考o2`和o2.a=o;//o`:内存现在看起来是这样的(如果我错了,请纠正我):
________                         ________    
|对象| |对象|
|________|   |   /               |________|
^        --/---------             ^
|         /          \            |
|        /            \           |
o/\o2
\-->a.a.a