views:

371

answers:

1

Hello All,

I would like to know the most efficient way of emptying an associative array without causing any memory leak (the values are js objects)

Just to be sure, I have tried iterating through the elements of the array, calling 'delete' operation for each element object, so that all of the element objects will become candidates for garbage collection. Finally, I reset the assoc array using: somearray = {};

Is it necessary to call the 'delete' operation for each of the element? Or does resetting the variable value suffice so that all elements will be garbage collected?

Thanks!

+1  A: 

As long as neither your objects nor the associative array are referenced anywhere else, resetting the array should do it.

Calling delete on each object is only necessary in IE6 on a DOM element. See here

SLaks
Hi, thanks for the response. However, I would like to clarify my concern further.I have an associative array that is used for storing GMarker objects. After adding each GMarker object to the array, it is also added to the map (using GMap2.addOverlay() method)When I want to remove all markers from the map, I would simply have to call GMap2.clearOverlays() method. But aside from this, I would also have to clear the associative array.Does this mean that when I reset the variable, all contained objects will eventually be garbage collected?Thanks in advance
Erwin Paglinawan
Yes, they will. In general, the best way to find out is to run your logic a million times in a loop, and loop at memory usage in Task Manager.
SLaks
I see. I'll try it, then. Thanks for the help!
Erwin Paglinawan
Why was this downvoted?
SLaks
I was also wondering the same thing. Who downvoted this answer? Actually, I was trying to add 1 point but I got the "you have less than 15 reps" warning message that time.
Erwin Paglinawan