• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Dynamic script generation and memory leaks ...

An interesting piece by Neil Fraser shows that using JSON-P with generated script nodes can be quite a memory leak. Normally you would add information returned from an API in JSON-P with a generated script node:


PLAIN TEXT
JAVASCRIPT:

  
      script = document.createElement( iscript);
  
        script.src = http://example.com/cgi-bin/jsonp?q=What+is+the+meaning+of+life%3F;
  
        script.id = JSONP;
  
        script.type = ext/javascript;
  
        script.charset = utf-8;
  
        // Add the script to the head, upon which it will load and execute.
  
        var head = document.getElementsByTagName(head)[0];
  
        head.appendChild(script)

The issue there is that you clog up the head of the document with lots of script nodes, which is why most libraries (like the YUI get() utility) will add an ID to the script element and remove the node after successfully retrieving the JSON data:


PLAIN TEXT
JAVASCRIPT:

 
      var script = document.getElementById(JSONP);
  
      script.parentNode.removeChild(script);

The issue with this is that browsers do remove the node but fail to do a clean garbage collection of the JavaScript at the same time. This means to cleanly remove the script and its content, you need to also delete the properties of the script by hand:


PLAIN TEXT
JAVASCRIPT:

  
 
        var script;
  
        while (script = document.getElementById(JSONP)) {
  
          script.parentNode.removeChild(script);
 
          // Browsers won garbage collect this object.
  
          // So castrate it to avoid a major memory leak.
  
          for (var prop in script) {
  
            delete script[prop];
  
          }
 
        }

 Original Source:
http://ajaxian.com/archives/dynamic-script-generation-and-memory-leaks

AddThis Social Bookmark Button

Posted at 09:16:18 am | Permalink | Posted in JSON  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange