理解并解决IE的内存泄漏方式

http://birdshome.cnblogs.com/archive/2006/06/17/cross_page_leaks.html

code sample:

html>
    
<head>
        
<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>
        
<div id="hostElement"></div>
    
</body>
</html>

阅读更多
个人分类: Developer
上一篇SQL Server数据库开发的二十一条军规
下一篇SQL SERVER实用经验技巧集
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭