SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Memory Leaks in JavaScript

    Can a script cause memory leaks? Or does JavaScript have a build in garbage collector like Java?

    More specifically, I have a global array variable which I often need to overwrite with

    arrGlobalArray = new Array();
    arrGlobalArray[0] = "new item";

    what happens when I renew the array with

    arrGlobalArray = new Array();
    arrGlobalArray[0] = "different item";

    Browser is IE6 if that helps.

  2. #2
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm fairly sure that it will get totally replaced, and the prev. variable is erased from memory right away

    Are you having problems with a particular scipt causing memory problems?

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An interesting question - but one that is probably fairly academic unless you were creating lots of really big objects or strings in your script.

    It really is up to the client I guess - as it is the client browser that has the built in javascript interpreter - so I am sure that garbage collection will vary from browser to browser. That said, you would hope that, as web pages only hold their state while the page is actually open, the browser would be sane enough to destroy all objects and release memory once the user has left the page.

    Anyway, I did a search in google and it came up with some interesting results:
    http://www.google.com/search?sourcei...age+collection

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    northern Maine
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I remember reading that typically after 10 object creations, a garbage collector is run. (At least in the Mozilla JS engine).
    Jason - Contact Me
    Supermoderator @ CodingForums


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •