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

Here is another piece of code. Instead of appending the script inline, we attach it externally:

[Exhibit 4 - Leak test with a closure]
·미리보기 | 소스복사·
  1. <html>   
  2. <head>   
  3. <script type="text/javascript">   
  4.     function LeakMemory(){   
  5.         var parentDiv = document.createElement("div");   
  6.                           parentDiv.onclick=function(){   
  7.             foo();   
  8.         };   
  9.   
  10.         parentDiv.bigString =    
  11.           new Array(1000).join(new Array(2000).join("XXXXX"));   
  12.     }   
  13. </script>   
  14. </head>   
  15. <body>   
  16. <input type="button"    
  17.        value="Memory Leaking Insert" onclick="LeakMemory()" />   
  18. </body>   
  19. </html>  

If you don't know what a closure is, there are very good references on the web where you may find it. Closures are very useful patterns; you should learn them and keep them in your knowledge base.

And here is the graph that shows the memory leak. This is somewhat different from the former examples. The anonymous function assigned to parentDiv.onclick is a closure that closes over parentDiv, which creates a circular reference between the JS world and DOM and creates a well-known memory leakage issue:

To generate leak in the above scenario, we should click the button, refresh the page, click the button again, refresh the page and so on.

Clicking the button without a subsequent refresh will generate the leak only once. Because, at each click, the onclick event of parentDiv is reassigned and the circular reference over the former closure is broken. Hence at each page load there is only one closure that cannot be garbage collected due to circular reference. The rest is successfully cleaned up.

번호 제목 글쓴이 날짜 조회 수
217 iframe auto resize (cross browsing) 황제낙엽 2011.05.13 658
216 Javascript CORS/XSS 극복하는(피하는) 방법 file 황제낙엽 2017.07.31 648
» [JavaScript Tutorials] Introducing the closure (해석중) 황제낙엽 2009.04.10 555
214 입력받은 날짜와 현재 날짜와의 비교 함수 황제낙엽 2019.08.02 500
213 Javascript 를 사용하여 Binary File 읽기 황제낙엽 2010.09.29 500
212 url encode & decode 황제낙엽 2011.10.30 469
211 자바스크립트로 서버의 XML파일을 접근 (실패했슴) 황제낙엽 2005.12.11 444
210 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 412
209 Defining classes and inheritance (클래스 정의와 상속) 황제낙엽 2011.03.24 392
208 Javascript 내장객체 String 황제낙엽 2007.04.10 392
207 JavaScript Array.push Performance 황제낙엽 2011.11.21 380
206 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 337
205 Faster JavaScript Memoization For Improved Application Performance 황제낙엽 2011.11.04 333
204 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 324
203 String Performance: Getting Good Performance from Internet Explorer (IE7) 황제낙엽 2011.11.24 312
202 window.postMessage() 황제낙엽 2020.07.29 303
201 unshift() Method 황제낙엽 2009.03.02 287
200 CORS(Cross-Origin Resource Sharing) - 5 file 황제낙엽 2017.03.07 261
199 Jasmine 테스트 및 CI 구축 가이드 황제낙엽 2016.11.16 254
198 정규식 정리 황제낙엽 2008.11.24 252