打开任务管理器,找到这个页面的进程。点击图片显示弹出层。再点击图片关闭。重复几次,会看到内存彪升。还有:拖动和关闭也会使内存上升我希望关闭之后所占用的内存都会自动释放<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
<style type="text/css">
*{margin:0;padding:0}
</style>
</head>
<body>
<img src="http://localhost/images/index_ad5.gif"  onclick='A.call(this)' />
<script>
function Move(o, e){
var e = window.event || e;
var _sy = e.clientY;
var _sx = e.clientX;
var _xy = parseInt(o.offsetTop) + o.height / 2;
var _xx = parseInt(o.offsetLeft) + o.width / 2;

document.onmouseup = function(){
this.onmousemove = null;
}
if(e.preventDefault){
e.preventDefault();
}
document.onmousemove = function(e){
var e = window.event || e;
if(document.all && e.button == 0){
this.onmousemove = null;
return false;
};
o.style.top = _xy + e.clientY - _sy + "px";
o.style.left = _xx + e.clientX - _sx + "px";
o.setAttribute('IsMove', 'true');
}
}
function A(){
var Bind = function(object, fun) {
return function() {
return fun.apply(object, arguments);
}
}
var w = document.createElement("div");
with(w.style){
position = 'absolute';
w._resize = Bind(w, function(){
zIndex = '10000';
width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
height =Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
position="absolute";
left = '0';
top = '0';
background = '#FFFFFF';
filter = 'Alpha(opacity=70)';
opacity = '0.7';
});
w._resize();
window.attachEvent("onresize", w._resize); 
}
document.body.appendChild(w);
var ig = new Image();
ig.src = this.src;
ig.setAttribute('unselectable', 'on');
ig.setAttribute('title', '点击关闭图片');
with(ig.style){
position = 'absolute';
zIndex = '10001';
left = '50%';
top = '50%';
marginLeft = - (ig.width+10) / 2 + 'px';
marginTop = - (ig.height+10) / 2 + 'px';
border = '1px solid #999999';
padding='4px'
background='#FFFFFF'
cursor='move'
}
ig.onmousedown = function(e){
ig.setAttribute('IsMove', 'false');
return Move(this, e);
}
ig.onclick = function(){
if(this.getAttribute('IsMove') == 'false'){
document.body.removeChild(this);
document.body.removeChild(w);
ig = w = null;
}
}
document.body.appendChild(ig);
}
</script>
</body>
</html>

解决方案 »

  1.   

    <img src="http://localhost/images/index_ad5.gif"  onclick='A.call(this)' />你每次click都执行一次 A ,每次都会创建一个 w 和 ig ,将这部分优化一下先~`````
      

  2.   

    document.body.removeChild(this);
    document.body.removeChild(w);
    因为每次关闭我都移除了节点啊,所以每次当然要创建
      

  3.   

    一点也不恐怖啊
    我关闭点开拖动许久
    才增加 0.01GB的内存占用
    CPU的话页面的JS DOM运算都是这样的
    唯一要改的就是 DOM的创建删除操作 看下来除了第一次显示的创建外 其他的只要设置显示和隐藏就可以了
      

  4.   

    这个是浏览器内存回收机制所决定,不是说你把对象remove掉了或者置null了,浏览器就立即回收
    内存回收是需要满足一定条件的,比如当内存不足时,浏览器会自动回收
    当浏览器最小化时(当然也包括关闭了),会自动回收js中显示调用回收方法:
    CollectGarbage();//这个我刚刚试了下,但是没有明显的变化
      

  5.   

    我的,占用N多的内存,CUP上到100%也没事啊,别人用得好好的。
      

  6.   


    顶!
    每次生成一个对象然后removeChild()时这个对象并未在内存销毁(除非按了F5或者关闭了页面),而且这个内存会被系统管理但是不会被释放。以前看到说JS有个没有被公布的释放内存的方法,可是释放为NULL的对象(没试过),所以你的优化需要避免大量的新建和赋值操作。另外var Bind = function(object, fun) {
    这种方式也少用,每使用一次就会在内存中生成一个对象实例。可以考虑用prototype进行扩展然后调用