delete image in Firefox

Discussion in 'Javascript' started by Yun, Jul 15, 2011.

  1. Yun

    Yun Guest

    Hi folks,

    I have a problem trying to delete some images that I previously load
    in one array. My code looks like this :

    // This is a global var
    images = new Array();

    function load_images() {

    var time = new Date().getTime();

    for (var i=0; i<=nimages, i++) {
    delete images;
    images = new Image();
    images.src = image_path+i+".png?d="+time
    }

    At the end of the image name I set ".png?d=124151353546". This is
    because I update the images every 5 minutes and the function is called
    again. If I don't change the name of the image (using this trick ?
    =....) the browser could think that the image is the same and it won't
    change anything.

    The problem I have using this code is that the image is not destroyed
    by the browser (Firefox 4 in my case). After 3 or 4 hours, the web
    site takes the whole RAM and I can't continue working.

    Why delete images is not working ? or How could I do to destroy the
    images I won't use again ?
    Thank you for your help,

    Yun
     
    Yun, Jul 15, 2011
    #1
    1. Advertisements

  2. Yun

    Tom de Neef Guest



    What happens when you use
    for (var i=0; i<nimages, i++) {
    images.src = image_path+i+".png?d="+time
    }

    Tom
     
    Tom de Neef, Jul 16, 2011
    #2
    1. Advertisements

  3. Yun

    Yun Guest

    Are you suggesting to not delete the object Image ? Mmmmhhh... I will
    try it next Monday and I will let you know.

    For some reason, the browser (Firefox) keeps the images in memory. I
    think I will have the same problem if I don't delete the object.
    Anyway, I don't see the logic of all of this.

    Thank you Tom, I will post the result as soon as possible.
    Yun
     
    Yun, Jul 16, 2011
    #3


  4. Is the problem that the browser is caching stagnant images?

    That's nothing to do with javascript and therefore there is nothing
    that you can do about it.

    In Firefox address bar enter "about:cache" and see details about
    memory usage (refresh the page as your image refreshes) - does that
    indicate that your images are being cached in memory by Firefox - and
    that the images are not javascript memory leaks?

    warwound
     
    Martin Pearman, Jul 16, 2011
    #4
  5. Yun

    Yun Guest

    Hello Martin,

    Thank you for your help... I didn't know this option "about:cache", it
    is very useful.
    I realized that your are right, the stagnant images are cached in
    memory by Firefox and I lost the reference to them. That shocks me !

    So, even if I destroy the object Image, the image is still cached by
    the browser and I can not do anything about it. That's new, I didn't
    have this problem using Firefox 3.6...

    Another thing :

    image aux = new Image();
    aux.src = 'image_1.png'
    aux.src = 'image_2.png'

    In this case, and if I understand correctly, "image_1.png" will be
    also cached by the browser and I can not do anything to flush from the
    cache. Is that right ?

    Thank you for your help guys,
    Yun
     
    Yun, Jul 17, 2011
    #5
  6. Yun

    Yun Guest

    Hello Martin,

    Thank you for your help... I didn't know this option of checking the
    cache, it is very useful.
    I realized that your are right, the stagnant images are cached in
    memory by Firefox and I lost the reference to them. That shocks me !

    So, even if I destroy the object Image the image is still cached by
    the browser and I can not do anything about it. This is new, I didn't
    have this problem using Firefox 3.6...

    Another thing.

    image aux = new Image();
    aux.src = 'image_1.png'
    aux.src = 'image_2.png'

    In this case and if I understand correctly, "image_1.png" will be also
    cached by the browser and I can not do anything to flush from the
    cache ?

    Thank you for your help,
    Yun
     
    Yun, Jul 17, 2011
    #6
  7. Yun

    dhtml Guest


    Use Expires or Cache-Control HTTP headers.

    http://www.mnot.net/cache_docs/

    The delete operator doesn't remove objects from browser memory. You've
    removed a reference to the image and if no other references exist, the
    browser may reclaim that memory but it doesn't have to.

    I've seen similar in Apple's "Advanced Best Practices":

    http://developer.apple.com/library/...nceptual/JSCodingGuide/Advanced/Advanced.html

    | Release initialization functions. Code that’s called once
    | and never used again can be deleted after its execution.
    | For instance, deleting a window’s onload handler function
    | releases any memory associated with the function, like this:
    |
    |
    | var foo = function()
    | {
    | // code that makes this function work
    | delete foo;
    | }
    |
    | window.addEventListener('load', foo, false);

    Which, if they'd tested it, won't delete `foo` because variables have
    an internal attribute that in ECMAScript 3 is called [[DontDelete]].
    Attempts to `delete` a variable in ES5 in strict mode result in
    SyntaxError.

    What they could have done instead is to remove the event listener and
    set `foo=null` (though in most cases that isn't going to make a hill
    of beans).

    Use Expires or Cache-Control on the images sent from the server.

    http://www.mnot.net/cache_docs/
     
    dhtml, Jul 18, 2011
    #7
  8. Yun

    SAM Guest

    Le 17/07/11 05:00, Yun a écrit :
    can you do something for all the images already seen elsewhere and also
    cached ?
     
    SAM, Jul 18, 2011
    #8
  9. Yun

    Ry Nohryb Guest

    X-No-Archive: Yes

    I've seen that same problem before, in some version of iOS IIRC.
    Perhaps if you do a location.reload() every now and then, it may work.
    At least it did on the iphone (iirc). A setInterval(reload,
    halfAnHour) or something...
     
    Ry Nohryb, Jul 18, 2011
    #9
  10. Yun

    dhtml Guest

    I'm interested to know if you have tried this and what the results
    were. I've not experienced the problem you are having and so never
    tried this solution. Theoretically, I think it should work.
     
    dhtml, Jul 22, 2011
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.