您的位置:首页 > Web前端 > JavaScript

js 不能控制内存的释放,delete、CollectGarbage()都不行

2007-10-21 16:55 309 查看
google_ad_client = "pub-5033576919944123";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2007-10-24: csdn.blog
google_ad_channel = "8548491739";

一直很疑惑,delete到底能不能用来释放内存?今天看到如下一段代码,执行后发现脚本脚本执行很慢(后发现原来是alert搞的鬼)

function test()

{

var str = "1"

for (var i = 0; i<25; i++)

{

str += str;

}

alert(str);

delete str;

}

&amp;lt;!--strTest = "1";for ( var i = 0; i &amp;lt; 26; i ++ ){ strTest += strTest;}alert(strTest);delete strTest;//--&amp;gt;

关键是delete删除变量之外并没有看出释放内存的迹象(在页面执行到关闭前,内存使用量都比较稳定,页面关闭后才猛地释放大量内存),这验证了"delete从对象中删除一个属性,或从数组中删除一个元素。"(script56) 后google出一个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. ) 效果仍不明显.

google_ad_client = "pub-5033576919944123";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2007-10-24: csdn.blog
google_ad_channel = "8548491739";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: