sitelink1  
sitelink2  
sitelink3  
sitelink4 http://1 
extra_vars4 ko 
extra_vars5 http://www.javascriptkit.com/javatutors/closuresleak/index3.shtml 
extra_vars6 sitelink1 

All of the patterns shown below are described in detail in Justing's article. I'm going through them just for the sake of completeness:

[Exhibit 5 - Circular reference because of expando property]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4.     var myGlobalObject;   
  5.   
  6.     function SetupLeak(){   
  7.         //Here a reference created from the JS World    
  8.         //to the DOM world.   
  9.         myGlobalObject=document.getElementById("LeakedDiv");   
  10.   
  11.         //Here DOM refers back to JS World;    
  12.         //hence a circular reference.   
  13.         //The memory will leak if not handled properly.   
  14.         document.getElementById("LeakedDiv").expandoProperty=   
  15.                                                myGlobalObject;   
  16.     }   
  17. </script>   
  18. </head>   
  19. <body onload="SetupLeak()">   
  20. <div id="LeakedDiv"></div>   
  21. </body>   
  22. </html>  

Here the global variable myGlobalObject refers to the DOM element LeakDiv; at the same time LeakDiv refers to the global object through its expandoProperty. The situation looks like this:

The above pattern will leak due to the circular reference created between a DOM node and a JS element.

Since the JScript garbage collector is a mark and sweep GC, you may think that it would handle circular references. And in fact it does. However this circular reference is between the DOM and JS worlds. DOM and JS have separate garbage collectors. Therefore they cannot clean up memory in situations like the above.

Another way to create a circular reference is to encapsulate the DOM element as a property of a global object:

[Exhibit 6 - Circular reference using an Encapsulator pattern]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4. function Encapsulator(element){   
  5.     //Assign our memeber   
  6.     this.elementReference = element;   
  7.   
  8.     // Makea circular reference   
  9.     element.expandoProperty = this;   
  10. }   
  11.   
  12. function SetupLeak() {   
  13.     //This leaks   
  14.     new Encapsulator(document.getElementById("LeakedDiv"));   
  15. }   
  16. </script>   
  17. </head>   
  18. <body onload="SetupLeak()">   
  19. <div id="LeakedDiv"></div>   
  20. </body>   
  21. </html>  

Here is how it looks like:

However, the most common usage of closures over DOM nodes is event attachment. The following code will leak:

[Exhibit 7 - Adding an event listener as a closure function]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4. window.onload=function(){   
  5.     // obj will be gc'ed as soon as    
  6.     // it goes out of scope therefore no leak.   
  7.     var obj = document.getElementById("element");   
  8.        
  9.     // this creates a closure over "element"   
  10.     // and will leak if not handled properly.   
  11.     obj.onclick=function(evt){   
  12.         ... logic ...   
  13.     };   
  14. };   
  15. </script>   
  16. </head>   
  17. <body>   
  18. <div id="element"></div>   
  19. </body>   
  20. </html>  

Here is a diagram describing the closure which creates a circular reference between the DOM world and the JS world.

The above pattern will leak due to closure. Here the closure's global variable obj is referring to the DOM element. In the mean time, the DOM element holds a reference to the entire closure. This generates a circular reference between the DOM and the JS worlds. That is the cause of leakage.

When we remove closure we see that the leak has gone:

[Exhibit 8- Leak free event registration - No closures were harmed]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4. window.onload=function(){   
  5.     // obj will be gc'ed as soon as    
  6.     // it goes out of scope therefore no leak.   
  7.     var obj = document.getElementById("element");   
  8.     obj.onclick=element_click;   
  9. };   
  10.   
  11. //HTML DOM object "element" refers to this function   
  12. //externally   
  13. function element_click(evt){   
  14.     ... logic ...   
  15. }   
  16. </script>   
  17. </head>   
  18. <body>   
  19. <div id="element"></div>   
  20. </body>   
  21. </html>  

Here is the diagram for the above code piece:

This pattern will not leak because as soon as the function window.onload finishes execution, the JS object obj will be marked for garbage collection. So there won't be any reference to the DOM node on the JS side.

And the last but not the least leak pattern is the "cross-page leak":

[Exhibit 10 - Cross Page Leak]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4. function LeakMemory(){   
  5.     var hostElement = document.getElementById("hostElement");   
  6.     // Do it a lot, look at Task Manager for memory response   
  7.     for(i = 0; i < 5000; i++){   
  8.         var parentDiv =   
  9.         document.createElement("<div onClick='foo()'>");   
  10.   
  11.         var childDiv =   
  12.         document.createElement("<div onClick='foo()'>");   
  13.   
  14.         // This will leak a temporary object   
  15.         parentDiv.appendChild(childDiv);   
  16.         hostElement.appendChild(parentDiv);   
  17.         hostElement.removeChild(parentDiv);   
  18.         parentDiv.removeChild(childDiv);   
  19.         parentDiv = null;   
  20.         childDiv = null;   
  21.     }   
  22.     hostElement = null;   
  23. }   
  24. </script>   
  25. </head>   
  26. <body>   
  27. <input type="button"    
  28.        value="Memory Leaking Insert" onclick="LeakMemory()" />   
  29. <div id="hostElement"></div>   
  30. </body>   
  31. </html>  

Since we observe memory leakage even in Exhibit 1, it is not surprising that this pattern leaks. Here is what happens: When we append childDiv to parentDiv, a temporary scope from childDiv to parentDiv is created which will leak a temporary script object. Note that document.createElement("<div onClick='foo()'>"); is a non-standard method of event attachment.

Simply using the "best practices" is not enough (as Justing has mentioned in his article as well). One should also adhere to standards as much as possible. If not, he may not have a single clue about what went wrong with the code that was working perfectly a few hours ago (which had just crashed unexpectedly).

Anyway, let us re-order our insertion. The code below will not leak:

[Exhibit 11 - DOM insertion re-ordered - no leaks]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4. function LeakMemory(){   
  5.     var hostElement = document.getElementById("hostElement");   
  6.     // Do it a lot, look at Task Manager for memory response   
  7.     for(i = 0; i < 5000; i++){   
  8.         var parentDiv =   
  9.           document.createElement("<div onClick='foo()'>");   
  10.   
  11.         var childDiv =   
  12.           document.createElement("<div onClick='foo()'>");   
  13.   
  14.         hostElement.appendChild(parentDiv);   
  15.         parentDiv.appendChild(childDiv);   
  16.         parentDiv.removeChild(childDiv);   
  17.         hostElement.removeChild(parentDiv);   
  18.   
  19.         parentDiv = null;   
  20.         childDiv = null;   
  21.     }   
  22.     hostElement = null;   
  23. }   
  24. </script>   
  25. </head>   
  26. <body>   
  27. <input type="button"    
  28.        value="Memory Leaking Insert" onclick="LeakMemory()" />   
  29. <div id="hostElement"></div>   
  30. </body>   
  31. </html>  

We should keep in mind that, although it is the market leader, IE is not the only browser in the world. And writing IE-specific non-standard code is a bad practice of coding. The counter-argument is true as well. I mean, saying "Mozilla is the best browser so I write Mozilla-specific code; I don't care what the heck happens to the rest" is an equally bad attitude. You should enlarge your spectrum as much as possible. As a corollary, you should write standards-compatible code to the highest extent, whenever possible.

Writing, "backwards compatible" code is "out" nowadays. The "in" is writing "forward compatible" (also known as standards compatible) code which will run now and in the future, in current and in future browsers, here and on the moon.

번호 제목 글쓴이 날짜 조회 수
177 익스플로러용 스크립트 디버거 (Script Debugger for Windows NT 4.0 and Later) 황제낙엽 2008.12.11 176
176 MS 익스플로러상에서 문제가 되는 Leak 모델 황제낙엽 2009.04.03 171
175 소스 보기 막기 황제낙엽 2005.11.18 168
174 문자열에서 역슬래시(backslash) 문자와 유니코드(Unicode)에 대한 고찰 file 황제낙엽 2021.06.03 160
173 [펌] 아사페릴의 사생활 - Javascript의 constructor 와 prototype 황제낙엽 2009.04.02 156
172 JAVASCRIPT REFERENCE 파일 file 황제낙엽 2005.11.22 153
171 CKEditor 3 JavaScript API Documentation 황제낙엽 2011.11.14 147
170 Java 버전의 JavaScript 엔진 라이노 (Rhino) 황제낙엽 2008.07.14 146
» [JavaScript Tutorials] More leakage patterns (해석중) 황제낙엽 2009.04.10 142
168 CORS 의 내용과 이에 대한 우회 방안들 file 황제낙엽 2021.12.05 139
167 CORS(Cross-Origin Resource Sharing) - 1 file 황제낙엽 2017.03.07 135
166 재사용 가능한 일회용 객체 황제낙엽 2008.08.08 133
165 inherits() 를 이용한 상속 황제낙엽 2012.07.18 129
164 HTTP 접근 제어 (CORS) 황제낙엽 2017.05.29 125
163 call() and apply() methods in Javascript 황제낙엽 2011.10.07 125
162 외부 라이브러리 (.js) 의 바람직한 동적 로딩 (The best way to load external JavaScript) 황제낙엽 2009.10.05 124
161 자동 형변환 (문자열 -> 숫자) 황제낙엽 2009.06.25 124
160 자바스크립트 학습용 유튜브 강의 (드림코딩 by 엘리) 황제낙엽 2021.03.07 122
159 무지개링크 (rainbowlink) file 황제낙엽 2005.07.16 122
158 Rhino 와 env.js 를 사용해서 자바 서버에서 javascript 를 구동해보자 file 황제낙엽 2012.02.15 116