If a variable is stored (for example, global or part of some constant data structure) and the data it points to is large, and you want this data to have garbage collection, then you are right to assign something small to this variable , undefined or null or "" everything will work. What you are doing is clearing the big data link so that it has the right to garbage collection. If nothing else in your javascript refers to this data, it can be freed by the garbage collector. If something else has a link to it, then it cannot be released.
For example, if you have an array of 10,000 elements stored in a global variable:
var largeDataArray = new Array(10000);
And you filled most of the elements with data, then you can let this memory have the right to collect garbage by assigning it a different value, for example:
largeDataArray = null;
or if you still want this to be an array:
largeDataArray = [];
Note: variables that themselves go out of scope (for example, local variables in functions that are not part of a strong closure) or variables in objects that themselves go out of scope should not be cleared manually. When they go beyond or when the parent is deleted, the data contained inside will also have the right to garbage collection.
Thus, purging a variable is only required when you explicitly want to free the data that is stored in a long-lived variable, and usually it only makes sense to worry about it when the data is large or you have a lot that contain up to several megabytes of data (using memory for more high memory levels on smartphones are higher than on desktop browsers).
jfriend00 Dec 11 '11 at 21:20 2011-12-11 21:20
source share