setTimeout 和setInterval 內存溢出的问题.

Abin-2008 2010-07-31 11:35:54
我做了一个聊天室...即时刷新的.
前台我写的是隔5秒请求后台查询数据库有最新的留言.

然后我也写了一个针对IE的内存清理代码.

这个是隔5秒清除一个settimeout

function ajax_loadTable(){
//中间还有很多代码
if(settimeout_id!=null && settimeout_id!="")
{
clearTimeout(settimeout_id); //清除settimeout函數產生的內存消耗.
}
settimeout_id=setTimeout("ajax_loadTable()",5000); //設置自動刷新.
}

这个是清理IE内存垃圾的

$(doucment).ready(function(){
if(isIE){CollectGarbage("collect");} //清除運行之前的內存.
// 每隔 10 秒释放一次内存
if(isIE){window.setTimeout("CollectGarbage();",5000);}
ajax_loadTable();
});


问题就是:如何保证及时刷新 而且又能释放内存.不会产生内存溢出...
...全文
1033 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
Abin-2008 2010-08-04
  • 打赏
  • 举报
回复
你们自己用sIEve-0.0.8
测试IE内存溢出工具测试下...什么没有溢出...就算是用了clearTimeout一样会.
Abin-2008 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 mowanglijiang 的回复:]

应该是ajax代码有问题,我的跟本没有用过什么内存清理,开一整天也没有什么问题,你还是找找其它的ajax代码试试。
[/Quote]
这个ajax是jquery的....
Abin-2008 2010-08-04
  • 打赏
  • 举报
回复
为什么就是没有高人呢????

连带星的人都没有...
Abin-2008 2010-08-03
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 tidelgl 的回复:]

在AJAX返回成功后清理就可以保证了
[/Quote]
我有清理掉...
Abin-2008 2010-08-03
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 tidelgl 的回复:]

在AJAX返回成功后清理就可以保证了
[/Quote]
我现在变成这样的写法
function a()
{
$.ajax({
url:""....
dataType:"json",
success:function(data){b(data);}
})
}
function b(table_data)
{
var str="";
var settimeout_id;
$("div").empty();
for(var i=0;i<table_data.length;i++)
{
str+=table_data[i].message;....
}
$("div").prepend(str);
if(settimeout_id!=null || settimeout_id!="")
{

clearTimeout(settimeout_id); //清除settimeout函數產生的內存消耗.
// 每隔 5 秒释放一次内存
settimeout_id=null;
str=null;
if(isIE){
try{
CollectGarbage();
}
catch(e){
alert("sasas");
}
}
}
settimeout_id=setTimeout("a()",5000); //設置自動刷新.
}
Abin-2008 2010-08-03
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 reedseutozte 的回复:]

你在setTimeout中改变了Dom节点的innerHTML,又没有把对应的onclick.onchange之类的置为null阿,
[/Quote]
设置为null...那我要用的时候怎么办呢?
而且还有很多事件呢..
reedseutozte 2010-08-03
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 woshiguozhongbin 的回复:]

引用 16 楼 reedseutozte 的回复:

你在setTimeout中改变了Dom节点的innerHTML,又没有把对应的onclick.onchange之类的置为null阿,

设置为null...那我要用的时候怎么办呢?
而且还有很多事件呢..
[/Quote]
我的意思是在你修改html之前把Dom和其子节点的事件置为null,如果事件很多
你可以用微软的工具看一下,使那些事件引起泄露
上海程序员3 2010-08-03
  • 打赏
  • 举报
回复
应该是ajax代码有问题,我的跟本没有用过什么内存清理,开一整天也没有什么问题,你还是找找其它的ajax代码试试。
Abin-2008 2010-08-02
  • 打赏
  • 举报
回复
我就是想能不能用JavaScript來决绝setTimeout的内存溢出...
Abin-2008 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 reedseutozte 的回复:]

CollectGarbag这个API没什么用吧,刷新的时候自动回收
如果存在IE内存不断增长得情况,则是你的代码中存在内存泄漏
注意检查一下是否在闭包中定义Dom相应事件(onclick,onchange)如果有情在页面跳转前结束饮用
比如obj.onclick =null等
[/Quote]

我查询了就是setTimeout的问题...我的其他代码不会溢出...
Abin-2008 2010-08-02
  • 打赏
  • 举报
回复
JavaScript的大神呢????
我就是想知道你们是怎么样处理setTimeout的内存溢出的....
tidelgl 2010-08-02
  • 打赏
  • 举报
回复
在AJAX返回成功后清理就可以保证了
reedseutozte 2010-08-02
  • 打赏
  • 举报
回复
你可以用下载新版javascript memory leak detector工具:http://blogs.msdn.com/gpde/pages/javascript-memory-leak-detector-v2.aspx
检测一下你的网页是否存在DOM 事件泄露
reedseutozte 2010-08-02
  • 打赏
  • 举报
回复
你在setTimeout中改变了Dom节点的innerHTML,又没有把对应的onclick.onchange之类的置为null阿,
reedseutozte 2010-07-31
  • 打赏
  • 举报
回复
CollectGarbag这个API没什么用吧,刷新的时候自动回收
如果存在IE内存不断增长得情况,则是你的代码中存在内存泄漏
注意检查一下是否在闭包中定义Dom相应事件(onclick,onchange)如果有情在页面跳转前结束饮用
比如obj.onclick =null等
kyzy_yy_pm 2010-07-31
  • 打赏
  • 举报
回复
上面是我做的一个这方面的东西,不知道你是否排斥这种方式
kyzy_yy_pm 2010-07-31
  • 打赏
  • 举报
回复
注意数据库

//a.html(发送消息)

<textarea id="msg" rows="5" cols="30"></textarea>
<input type="button" value="发送" onclick="send()"><span id="sendmsg"></span>

<script src="../jquery.js"></script>
<script>
function send(){
var msg = document.getElementById("msg").value;
if(!msg)document.getElementById("sendmsg").innerHTML = "消息不能为空";
document.getElementById("sendmsg").innerHTML = "正在发送...";
$.post("server.php", {"msg": msg, "rand": Math.random()}, function(data){
if(data == "1"){
document.getElementById("sendmsg").innerHTML = "发送成功!";
}else{
document.getElementById("sendmsg").innerHTML = "发送失败!";
}
});
}
</script>

//b.html

<div id="div"></div>

<script src="../jquery.js"></script>
<script>
function text_replace(text){
var l = text.length;
for (var i = 0; i < l; i++) {
if (text.charAt(i) == "%"){
if(text.charAt(i+1) == "2"){
if(text.charAt(i+2) == "3"){
text = text.replace("%23","#");
}
if(text.charAt(i+2) == "4"){
text = text.replace("%24","$");
}
if(text.charAt(i+2) == "6"){
text = text.replace("%26","&&");
}
if(text.charAt(i+2) == "B"){
text = text.replace("%2B","+");
}
if(text.charAt(i+2) == "C"){
text = text.replace("%2C",",");
}
if(text.charAt(i+2) == "F"){
text = text.replace("%2F","/");
}
}
if(text.charAt(i+1) == "3"){
if(text.charAt(i+2) == "F"){
text = text.replace("%3F","?");
}
if(text.charAt(i+2) == "B"){
text = text.replace("%3B",";");
}
if(text.charAt(i+2) == "D"){
text = text.replace("%3D","=");
}
if(text.charAt(i+2) == "A"){
text = text.replace("%3A",":");
}
}
if(text.charAt(i+1) == "4"){
if(text.charAt(i+2) == "0"){
text = text.replace("%40","@");
}
}
}
}
return text;
}

var filemtime = 0;
function test(){
$.post("server.php", {"filemtime": filemtime, "rand": Math.random()}, function(data){
if(data){
data = eval("(" + data + ")");
filemtime = data["filemtime"];
document.getElementById("div").innerHTML = text_replace(decodeURI(data["msg"]));
setTimeout("test()", 1000);
}else{
alert("error");
}
});
}

test();
</script>

//server.php

<?php
function DBConnect(){
mysql_connect('192.168.1.2', 'db_user', 'db_pass');
mysql_select_db(db_name');
mysql_query('set names gbk');
}

if(isset($_POST['msg'])){
$msg = iconv('utf-8', 'gbk', htmlspecialchars($_POST['msg']));
DBConnect();
mysql_query('insert into test(msg) values("'.$msg.'")');
if(mysql_insert_id()){
$fp = fopen('msgfile.txt', 'a');
fputs($fp, $msg, 1);
echo '1';
exit;
}
echo '2';
exit;
}

$filemtime = isset($_POST['filemtime']) && $_POST['filemtime']? $_POST['filemtime']: filemtime('msgfile.txt');

while(1){
if($filemtime == filemtime('msgfile.txt')){
sleep(1);
clearstatcache();
}else{
DBConnect();
$rs = mysql_query('select msg from test order by id desc limit 0, 1');
if($row = mysql_fetch_assoc($rs)){
$msg = rawurlencode(iconv('gbk', 'utf-8', $row['msg']));
}
echo json_encode(array('filemtime' => filemtime('msgfile.txt'), 'msg' => $msg));
exit;
}
}


?>
Abin-2008 2010-07-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kyzy_yy_pm 的回复:]

用php么?如果是的话,请to me
[/Quote]
兄台怎么样联系你呢????
Abin-2008 2010-07-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kyzy_yy_pm 的回复:]

用php么?如果是的话,请to me
[/Quote]
是的.
kaifadi 2010-07-31
  • 打赏
  • 举报
回复
高级版JS问题,MARK!
加载更多回复(6)

87,917

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧