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

Andrew Ip andrewsyip at gmail.com
Wed Sep 23 03:08:33 CDT 2009


Hey Prashant,

It seems like you're orphaning all the values that were put into your  
"valueList" array when you delete the reference to the "hash" object.   
Note that the delete operator in JavaScript basically just sets a  
variable's value to uninitialized.  Try emptying out the valueList  
array first, then delete the empty valueList array, and finally  
deleting the "hash" object afterwards.  You should see a difference.

On another note, I'm not sure what you're trying to do with your  
Hashtable class, but JavaScript already allows you to use objects as  
associative arrays.  Check out the following:

http://www.quirksmode.org/js/associative.html

-andrew

On 2009-09-22, at 11:46 PM, Prashant Dahiya wrote:

> 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
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript




More information about the Javascript mailing list