87,925
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<script language="javascript">
var arr = new Array();
function c()
{
var div = document.createElement("DIV");
document.body.appendChild(div);
div.innerHTML = '<input type="button" value="从内部删除" onclick="f()"/><div style="position:absolute;top:110;left:110;width:1000px;height:1000px;-moz-opacity:0.07;filter:alpha(opacity=7);background-color:#000000"></div>';
arr.push(div);
abcd.innerText = arr.length;
}
function f()
{
var tmp = arr.pop();
while(tmp!=null)
{
// for(var i = tmp.childNodes.count-1; i >=0 ; i--)
// {
// tmp.removeChild(tmp.childNodes[i]);
// }
document.body.removeChild(tmp);
tmp = arr.pop();
}
abcd.innerText = arr.length;
}
function g()
{
CollectGarbage();
}
</script>
</head>
<body>
<input type="button" value="添加" onclick="c();" />
<input type="button" value="删除" onclick="f();" />
<input onclick="g()" type="button" value="释放内存" />
<div id="abcd"></div>
典型的 半透明层 内存泄露问题:<br />
测试:(必须在IE下测试才能发现问题)<br />
1、依次执行“添加”“删除”“释放内存”,如此反复多次,发现内存可以正常释放。<br />
2、依次执行“添加”“从内部删除”“释放内存”,如此反复多次,发现内存泄露无限制增长。
</body>
</html>
<div id="a">
<div id="b" style="你的透明度代码"></div>
</div>
a.innerText = "";
就一切搞定了,至少我测试的时候是 b 占用的大量内存可以正确释放。<html>
<head>
<script language="javascript">
if(typeof(HTMLElement)!="undefined" && !window.opera)
{
HTMLElement.prototype.insertAdjacentHTML=function(where, html)
{
var e=this.ownerDocument.createRange();
e.setStartBefore(this);
e=e.createContextualFragment(html);
this.insertAdjacentElement(where,e);
};
HTMLElement.prototype.insertAdjacentElement=function(where, e)
{
switch (where)
{
case 'beforeBegin': this.parentNode.insertBefore(e, this);break;
case 'afterBegin': this.insertBefore(e, this.firstChild); break;
case 'beforeEnd': this.appendChild(e); break;
case 'afterEnd':
if(!this.nextSibling) this.parentNode.appendChild(e);
else this.parentNode.insertBefore(e, this.nextSibling); break;
}
};
};
var arr = new Array();
function c()
{
var id = "mm_"+ (new Date().getTime().toString(36));
document.getElementById("mm").insertAdjacentHTML("beforeEnd", '<div id="'+ id +'"><input type="button" value="从内部删除" onclick="f()"/><div style="position:absolute;top:110;left:110;width:1000px;height:1000px;-moz-opacity:0.07;filter:alpha(opacity=7);background-color:#000000"></div></div>')
arr[arr.length] = document.getElementById(id);
abcd.innerHTML = arr.length;
}
function f()
{
arr.length = 0;
document.getElementById("mm").innerHTML = "";
}
function g()
{
CollectGarbage();
}
</script>
</head>
<body>
<input type="button" value="添加" onclick="c();" />
<input type="button" value="删除" onclick="f();" />
<input onclick="g()" type="button" value="释放内存" />
<div id="abcd"></div>
典型的 半透明层 内存泄露问题:<br />
测试:(必须在IE下测试才能发现问题)<br />
1、依次执行“添加”“删除”“释放内存”,如此反复多次,发现内存可以正常释放。<br />
2、依次执行“添加”“从内部删除”“释放内存”,如此反复多次,发现内存泄露无限制增长。
<div id="mm"></div>
</body>
</html>
HTMLElement.prototype.insertAdjacentHTML = function(where, html) {
//Meizz
var e = this.ownerDocument.createRange();
e.setStartBefore(this);
e = e.createContextualFragment(html);
this.insertAdjacentElement(where, e);
};
HTMLElement.prototype.insertAdjacentElement = function(where, e) {
//Meizz
switch (where) {
case 'beforeBegin' : this.parentNode.insertBefore(e, this); break;
case 'afterBegin' : this.insertBefore(e, this.firstChild); break;
case 'beforeEnd' : this.appendChild(e); break;
case 'afterEnd' :
if (!this.nextSibling) this.parentNode.appendChild(e);
else this.parentNode.insertBefore(e, this.nextSibling);
break;
}
};