성능과 튜닝 JScript Memory Leaks

황제낙엽 2011.11.28 15:47 조회 수 : 69

sitelink1  
sitelink2  
sitelink3  
sitelink4 http://1 
extra_vars4 ko 
extra_vars5 http://javascript.crockford.com/memory/leak.html 
extra_vars6 sitelink1 

JScript Memory Leaks

Douglas Crockford
www.crockford.com

 

When a system does not correctly manage its memory allocations, it is said to leak memory. A memory leak is a bug. Symptoms can include reduced performance and failure.


Microsoft's Internet Explorer contains a number of leaks, the worst of which is an interaction with JScript. When a DOM object contains a reference to a JavaScript object (such an event handling function), and when that JavaScript object contains a reference to that DOM object, then a cyclic structure is formed. This is not in itself a problem. At such time as there are no other references to the DOM object and the event handler, then the garbage collector (an automatic memory resource manager) will reclaim them both, allowing their space to be reallocated. The JavaScript garbage collector understands about cycles and is not confused by them. Unfortunately, IE's DOM is not managed by JScript. It has its own memory manager that does not understand about cycles and so gets very confused. As a result, when cycles occur, memory reclamation does not occur. The memory that is not reclaimed is said to have leaked. Over time, this can result in memory starvation. In a memory space full of used cells, the browser starves to death.


We can demonstrate this. In the first program, queuetest1, we will create 10000 DOM elements (spans), and at the same time, delete all but the 10 most recent. When you run it with the Windows Task Manager's Performance display, you will observe that PF (Page File) Usage remains fairly constant. Changes on PF Usage can be an indicator of memory allocation inefficiency.


Next, run the second program, queuetest2. It does the same thing as queuetest1, except that it adds a click handler to each element. On Mozilla and Opera, the PF Usage is about the same, but on IE we see a steady increase as memory leaks away at a rate of about a megabyte per second. Often this leakage is unnoticable. But as Ajax techniques become more popular, with pages staying in place longer, being subjected to many changes, failure becomes common.


Since IE is unable to do its job and reclaim the cycles, it falls on us to do it. If we explicitly break the cycles, then IE will be able to reclaim the memory. According to Microsoft, closures are the cause of memory leaks. This is of course deeply wrong, but it leads to Microsoft giving very bad advice to programmers on how to cope with Microsoft's bugs. It turns out that it is easy to break the cycles on the DOM side. It is virtually impossible to break them on the JScript side.


When we are done with an element, we must null out all of its event handlers to break the cycles. All we have to do is assign null to each event handler's property. This can be done very specifically, or we can make a generic purge function.


The purge function takes a reference to a DOM element as an argument. It loops through the element's attributes. If it finds any functions, it nulls them out. This breaks the cycle, allowing memory to be reclaimed. It will also look at all of the element's descendent elements, and clear out all of their cycles as well. The purge function is harmless on Mozilla and Opera. It is essential on IE. The purge function should be called before removing any element, either by the removeChild method, or by setting the innerHTML property.

function purge(d) {
var a = d.attributes, i, l, n;
if (a) {
l = a.length;
for (i = 0; i < l; i += 1) {
n = a[i].name;
if (typeof d[n] === 'function') {
d[n] = null;
}
}
}
a = d.childNodes;
if (a) {
l = a.length;
for (i = 0; i < l; i += 1) {
purge(d.childNodes[i]);
}
}
}

So finally we run the third program, queuetest3. In it, the purge function is called just before deleting DOM elements.


Note that handlers that were added with attachEvent() will not be found and will continue to leak.


Update: Microsoft has released a fix to this problem: 929874. If you can be confident that all of your users have accepted the patch, then it is no longer necessary to purge. Unfortunately, it is not possible to determine that, so purging will probably remain necessary for the rest of IE6's product life.

This is the nature of the web. Fixing bugs doesn't make the bugs go away.


Update: Ok, IE7 didn't fix it. Maybe IE8 will fix it.

번호 제목 글쓴이 날짜 조회 수
157 자바스크립트의 쉬프트 연산자 (Shift Operator) 와 음수 (Negative) 이야기 황제낙엽 2012.05.31 726
156 연산자 (===, ==, >=, <=) 황제낙엽 2012.05.30 47
155 XMLHttpRequest 의 이벤트 onreadystatechange 황제낙엽 2012.05.30 412
154 JavaScript 재입문 황제낙엽 2012.05.29 50
153 진행 상황 추적하기(XMLHttpRequest.readyState) file 황제낙엽 2012.05.23 324
152 JavaScript Touch and Gesture Events iPhone and Android 황제낙엽 2012.04.12 337
151 Javascript ArrayBuffer ? Binary handling in javascript 황제낙엽 2012.03.19 218
150 오류:호출자(서버 응용 프로그램이 아닌 서버)가 사용될 수 없어서 사라졌습니다. file 황제낙엽 2012.03.14 1950
149 Alert 에서의 개행처리 황제낙엽 2012.03.09 80
148 env.js 사용시 부족한 부분 file 황제낙엽 2012.02.15 33
147 Rhino 와 env.js 를 사용해서 자바 서버에서 javascript 를 구동해보자 file 황제낙엽 2012.02.15 116
146 외부 라이브러리 (.js) 의 바람직하지 않은 동적 로딩 (eval함수 이용) 황제낙엽 2012.01.18 1851
145 IFrames and cross-domain security file 황제낙엽 2012.01.13 246
144 자바스크립트 숫자형 체크 함수 (isFinite() 함수 와 isNaN() 함수) 황제낙엽 2011.12.13 67
143 char to hex string 황제낙엽 2011.11.29 206
142 Understanding delete 황제낙엽 2011.11.28 61
» JScript Memory Leaks 황제낙엽 2011.11.28 69
140 JavaScript Reserved Words 황제낙엽 2011.11.28 94
139 Reference Count (순환참조) 황제낙엽 2011.11.24 191
138 IE and Memory accumulation in Javascript (document.createElement()) file 황제낙엽 2011.11.24 30