[Javascript] Garbage Collection/ Freeing-up memory in Java script

Prashant Dahiya prashant.er at gmail.com
Wed Sep 23 01:46:59 CDT 2009


 Hi,



 I’m trying with this type of code (example):



function Hashtable() {



   this.valueList = new Array();



   var somePrivateMethod = function() { return 1; }



   this.putValue = function(name, value) {



    this.valueList[name] = value;



  }



}







for (var i = 0; i < 100000; i++)



{



                var hash = new Hashtable();



                hash.putValue("Key" + i, "Value" + i);



                delete hash;



                hash = null;



}



Running this script , it constantly increases the memory usage. Although the
object "hash" is deleted in each iteration and although it is set to null,
it does not seem to

be removed from memory. Calling $.summary() after execution has finished
shows more than 100000 functions still in memory. Running the script a
second time increases the memory again. More than 200000 functions are in
memory after the second execution.



Am I doing anything wrong here ?



Any ideas, how I can get rid of the unreferenced memory? In fact, AFAIK JS
engines do garbage collection automatically, any idea why does the java
script engine does not release the memory immediately although it is
unreferenced?

Is there a possibility to run the garbage collection of the java script
engine ($.gc doesn’t helped much)?



Regards,

P



More information about the Javascript mailing list