[ale] Possible javascript memory leak?

James Sumners james.sumners at gmail.com
Thu May 3 08:31:55 EDT 2012


The browser is probably retaining all of the images in memory. I would
try removing the image element and adding a new one each time:

$(document).ready(function()
{
    var $imgContainer = $('#img-container'),
        r = 0,
        refreshId = {};

    refreshId = setInterval( function()
    {
        r = (-0.5)+(Math.random()*(100));
        console.log(r);
        $imgContainer.replaceWith([
          '<img src="img.php?h=',
          r,
          '" id="img-container">'
        ].join(''));

        // re-cache the reference for the next run
        $imgContainer = $('#img-container');
    }, 3000);
});

On Thu, May 3, 2012 at 6:51 AM, Jim Lynch <ale_nospam at fayettedigital.com> wrote:
> I'm running a  small javascript script using the setInterval function.
> Under both Chrome and FF it leaks memory, or at least it keeps consuming
> memory until the system crashes, or freezes up.
>
> Here's the script.
>
> $(document).ready(function()
> {
>     var refreshId = setInterval( function()
>     {
>         var r = (-0.5)+(Math.random()*(100));
>         console.log(r);
>         $('#img-container').attr('src','img.php?h='+r);
>     }, 3000);
> });
>
> Is there something wrong with it?  Any suggestions on how to cure the
> problem without restarting the browser?  The php code reads a .png file,
> adds a bit of text to it and outputs it so it is displayed.
>
> Thanks,
> Jim.


-- 
James Sumners
http://james.roomfullofmirrors.com/

"All governments suffer a recurring problem: Power attracts
pathological personalities. It is not that power corrupts but that it
is magnetic to the corruptible. Such people have a tendency to become
drunk on violence, a condition to which they are quickly addicted."

Missionaria Protectiva, Text QIV (decto)
CH:D 59



More information about the Ale mailing list