小编use*_*346的帖子

使用jquery append()添加DOM元素似乎会泄漏内存?

刚刚遇到了一个客户,他们的Ajax webapp中存在巨大的内存泄漏问题.所以我决定创建以下测试用例来演示这个问题:

我在下面的例子中使用了drip/Sieve进行内存分析(http://home.orange.nl/jsrosman/)

案例很简单:我有以下javascript:

<html>
    <head>    
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js">
</script>    
</head>    
<script type="text/javascript">

    var lihtml = "<li class='green'>this is a test text</li>";

    function populatelist() {
        for (var i = 0; i < 10000; i++) {
            $('#listparent').append(lihtml);
        }    
    }    

    function clearlist() {
        $('#listparent').empty();
        if (typeof (CollectGarbage) == "function") {
            alert('gc');
            CollectGarbage();
        }    
    } 


    /* Alternative clearlist with Remove instead of Empty(), still leaks */
    function clearlist() {
        /* test remove the parent itself instead of empty below */
        $('#listparent').remove();
        $('body').append("<ul id='listparent'>"); …
Run Code Online (Sandbox Code Playgroud)

memory jquery dom memory-leaks

6
推荐指数
1
解决办法
3437
查看次数

标签 统计

dom ×1

jquery ×1

memory ×1

memory-leaks ×1