解决IE6中元素插入顺序造成的内存泄漏
2009-03-07 21:50
204 查看
下面是修改自微软的测试代码 点击MemoryFix后 Memory Leaking Insert也不会造成内存泄漏
</html>
<head>
<script>
function MemoryFix(){
var garbageBox=document.createElement("div");
garbageBox.style.display="none";
document.body.appendChild(garbageBox);
var createElement=document.createElement;
document.createElement=function(){
var obj=Function.prototype.apply.apply(createElement,[document,arguments]);
garbageBox.appendChild(obj);
return obj;
}
}
</script>
<script language="JScript">
function LeakMemory()
{
var hostElement = document.getElementById("hostElement");
// Do it a lot, look at Task Manager for memory response
for(i = 0; i < 5000; i++)
{
var parentDiv =
document.createElement("<div onClick='foo()'>");
var childDiv =
document.createElement("<div onClick='foo()'>");
// This will leak a temporary object
parentDiv.appendChild(childDiv);
hostElement.appendChild(parentDiv);
hostElement.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null;
childDiv = null;
}
hostElement = null;
}
function CleanMemory()
{
var hostElement = document.getElementById("hostElement");
// Do it a lot, look at Task Manager for memory response
for(i = 0; i < 5000; i++)
{
var parentDiv =
document.createElement("<div onClick='foo()'>");
var childDiv =
document.createElement("<div onClick='foo()'>");
// Changing the order is important, this won't leak
hostElement.appendChild(parentDiv);
parentDiv.appendChild(childDiv);
hostElement.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null;
childDiv = null;
}
hostElement = null;
}
</script>
</head>
<body>
<button onclick="LeakMemory()">Memory Leaking Insert</button>
<button onclick="CleanMemory()">Clean Insert</button>
<button onclick="MemoryFix()">MemoryFix</button>
<button onclick="CollectGarbage()">CollectGarbage</button>
<div id="hostElement"></div>
</body>
</html>
</html>
<head>
<script>
function MemoryFix(){
var garbageBox=document.createElement("div");
garbageBox.style.display="none";
document.body.appendChild(garbageBox);
var createElement=document.createElement;
document.createElement=function(){
var obj=Function.prototype.apply.apply(createElement,[document,arguments]);
garbageBox.appendChild(obj);
return obj;
}
}
</script>
<script language="JScript">
function LeakMemory()
{
var hostElement = document.getElementById("hostElement");
// Do it a lot, look at Task Manager for memory response
for(i = 0; i < 5000; i++)
{
var parentDiv =
document.createElement("<div onClick='foo()'>");
var childDiv =
document.createElement("<div onClick='foo()'>");
// This will leak a temporary object
parentDiv.appendChild(childDiv);
hostElement.appendChild(parentDiv);
hostElement.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null;
childDiv = null;
}
hostElement = null;
}
function CleanMemory()
{
var hostElement = document.getElementById("hostElement");
// Do it a lot, look at Task Manager for memory response
for(i = 0; i < 5000; i++)
{
var parentDiv =
document.createElement("<div onClick='foo()'>");
var childDiv =
document.createElement("<div onClick='foo()'>");
// Changing the order is important, this won't leak
hostElement.appendChild(parentDiv);
parentDiv.appendChild(childDiv);
hostElement.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null;
childDiv = null;
}
hostElement = null;
}
</script>
</head>
<body>
<button onclick="LeakMemory()">Memory Leaking Insert</button>
<button onclick="CleanMemory()">Clean Insert</button>
<button onclick="MemoryFix()">MemoryFix</button>
<button onclick="CollectGarbage()">CollectGarbage</button>
<div id="hostElement"></div>
</body>
</html>
相关文章推荐
- NSTimer保留目标对象引起循环引用造成内存泄漏解决办法
- InputMethodManager造成的内存泄漏问题及解决方法
- 【转】简单内存泄漏检测方法 解决 Detected memory leaks! 问题
- Android中使用Handler造成内存泄露的分析和解决
- IE6 Select元素无法被div等元素覆盖的bug解决办法
- 简单内存泄漏检测方法 解决 Detected memory leaks
- Android中使用Thread造成内存泄露的分析和解决
- 解决使用pthread_create函数造成的内存泄露
- (顺序有序表)插入元素
- 解决IE6下浮动后的元素使用负margin被隐藏.
- WebView内存泄漏解决方法
- delete一个void*可能会造成泄漏内存
- Android中使用Handler造成内存泄露的分析和解决
- 简单内存泄漏检测方法 解决 Detected memory leaks! 问题
- 简单内存泄漏检测方法 解决 Detected memory leaks! 问题
- android中共享全局数据的方法(static导致的内存泄漏问题,但其实并未解决)
- dell服务器内存插入的顺序总结
- 在一个List的指定位置插入一个元素对这个List中的已有的元素顺序的影响
- QuickReport报表Prepare之后造成内存泄露问题的解决方法
- 理解并解决IE的内存泄漏方式[翻译]