刚刚遇到了一个客户,他们的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)