百度空间 | 百度首页 
 
查看文章
 
在JavaScript中释放对象占用的内存
2008年02月23日 星期六 09:24


引用:
------------------------------------------------------------------------------------------------------------------
在JavaScript中释放对象占用的内存
今天为了提高客户端的运行效率,开始优化代码,先检查内存释放问题

找到了一个javascript的未公开函数CollectGarbage()

According to MSDN, CollectGarbage is an undocumented function of the language and it is used for testing only. However this is the only way to force the garbage collection in Javascript, setting a variable to null or deleting it will not destroy the resource until the browser engine is destroyed.



2003年12月26日 15:47
------------------------------------------------------------------------------------------------------------------

<SCRIPT LANGUAGE="JavaScript">
<!--
strTest = "1";
for ( var i = 0; i < 25; i ++ )
{
     strTest += strTest;
}
alert(strTest);
delete strTest;
CollectGarbage();
//-->
</SCRIPT>

我这里试下来每次都能立即释放内存的。

类别:网络技术 | 添加到搜藏 | 浏览() | 评论 (2)
最近读者:
 
网友评论:
1
2008年12月15日 星期一 12:37 | 回复
你的方法是无效的。delete不能直接删除变量,只能删除对象的属性或者数组的某个元素。delete方法有返回值,你可以输出看看。
 
2
2009年08月05日 星期三 16:34 | 回复
好像没什么用
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     
 
精彩相册
   
     

©2009 Baidu