在图片之间切换时发生Javascript内存泄漏(FireFox4) [英] Javascript memory leak when switching between images (FireFox4)

查看:140
本文介绍了在图片之间切换时发生Javascript内存泄漏(FireFox4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我认为,是一个相当微不足道的JavaScript。
如果我运行的非常快,所以情况更加恶化,FireFox 4中的内存分配不断增加。我试过这个在铬和内存似乎保持稳定。

这是一个FF4的问题,或者我有我构建我的JavaScript很差?



注意没有其他JS文件被加载在页面上。我正在运行FF的安全模式与禁用所有插件。

 < img id =heartbeatname =heartbeatsrc =/ web / resources /graphics/greylight.png/> 

< script type =text / javascript>

var hasTimedout = 1;
var lastPollTime = new Date();;
var maxDifference = 6000 * 2; //允许我们错过数据轮询而不会显示任何错误

函数heartbeat()
{
var curTime = new Date();

var diff = curTime.getTime() - lastPollTime.getTime();

if(diff> maxDifference&& hasTimedout == 0)
{
document.getElementById('heartbeat')。src ='/ web / resources / graphics /greylight.png';

hasTimedout = 1;

else if(diff< maxDifference&& hasTimedout == 1)
{
document.getElementById('heartbeat')。src ='/ web / resources /graphics/greenlight.png';

hasTimedout = 0;
}

toggle_visibility('heartbeat');


function toggle_visibility(id){
var e = document.getElementById(id);
if(e.style.display =='block')
e.style.display ='none';
else
e.style.display ='block';
}

setInterval(heartbeat(),20);
< / script>


解决方案

有关Javascript垃圾回收的一些信息:在JS GC上线程



特别兴趣(也许):


  • 使用删除语句。每当
    使用一个新的
    语句创建一个对象时,就把它与一个删除
    语句配对。这确保了

    对象相关联的所有内存(包括其属性名称
    )都可用于垃圾收集。
    在释放对象中讨论了
    语句。

  • 使用var关键字。在没有var关键字的情况下创建的任何变量
    都是在全局范围创建的
    ,并且是
    永远不符合垃圾
    集合的条件,从而显示内存泄漏的
    机会。 / li>


我只能得出结论,您应该尝试使用new关键字与delete语句配对创建对象,看看是否有所作为。

否则代码看起来很好。


I have, what I think, is a fairly trivial bit of javascript. If I run this really fast, so that the situation is exacerbated, memory allocation in FireFox 4 keeps increasing. I tried this in chrome and memory seems to remain stable.

Is this a FF4 issue or do I have I constructed my JavaScript poorly?

Note no other JS files are loaded on the page. I am running FF in "safe mode" with all addons disabled. No other tabs are loaded.

<img id="heartbeat" name="heartbeat" src="/web/resources/graphics/greylight.png" />

    <script type="text/javascript">

        var hasTimedout = 1;
        var lastPollTime = new Date();;
        var maxDifference = 6000 * 2; //allows us to miss one poll of the data without showing anything bad

        function heartbeat()
        {
            var curTime = new Date();

            var diff = curTime.getTime() - lastPollTime.getTime();

            if (diff > maxDifference && hasTimedout == 0)
            {
                document.getElementById('heartbeat').src = '/web/resources/graphics/greylight.png';

                hasTimedout = 1;
            }
            else if (diff < maxDifference && hasTimedout == 1)
            {
                document.getElementById('heartbeat').src = '/web/resources/graphics/greenlight.png';

                hasTimedout = 0;
            }

            toggle_visibility('heartbeat');
        }

        function toggle_visibility(id) {
           var e = document.getElementById(id);
           if (e.style.display == 'block')
              e.style.display = 'none';
           else
              e.style.display = 'block';
        }

        setInterval("heartbeat()",20);    
    </script>

解决方案

Some info on Javascript garbage collection: SO Thread on JS GC

Of particular interest (perhaps):

  • Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in "Freeing Objects."
  • Use the var keyword. Any variable created without the var keyword is created at the global scope and is never eligible for garbage collection, presenting the opportunity for a memory leak.

I can only conclude that you should try pairing your object creation using the "new" keyword with delete statements and see if that makes a difference.

Otherwise the code looks fine.

这篇关于在图片之间切换时发生Javascript内存泄漏(FireFox4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆