Circular Reference
function SetupLeak()
{
myGlobalObject = document.getElementById("LeakedDiv");
document.getElementById("LeakedDiv").expandoProperty = myGlobalObject;
}
{
myGlobalObject = document.getElementById("LeakedDiv");
document.getElementById("LeakedDiv").expandoProperty = myGlobalObject;
}
That is, Dom element holds the reference to the jscript scope object.
Closure
function AttachEvents(element)
{
element.attachEvent("onclick", ClickEventHandler);
function ClickEventHandler() { // This closure refs element}
}
function SetupLeak()
{
// The leak happens all at once
AttachEvents(document.getElementById("LeakedDiv"));
}
That is, Element holds reference to closure, but at the same time, the closure holds reference to the parent function's parameter.
{
element.attachEvent("onclick", ClickEventHandler);
function ClickEventHandler() { // This closure refs element}
}
function SetupLeak()
{
// The leak happens all at once
AttachEvents(document.getElementById("LeakedDiv"));
}
That is, Element holds reference to closure, but at the same time, the closure holds reference to the parent function's parameter.
Cross-Page Leaks
DOM Insertion Order Lead.
If we insert elements from parent to child, Thn No Memory Leak:
parentElement.appendChild(childElement);
childElement.appendChild(subChildElement);
But if we use it this way, Memory Leak will Happen:
childElement.appendChild(subChildElement);
parentElement.appendChild(childElement);
Pseudo-Leaks
本文深入探讨了内存泄漏的各种形式,包括循环引用、闭包导致的内存泄漏及跨页面泄漏等问题,并提出了伪泄漏的概念。
1126

被折叠的 条评论
为什么被折叠?



