Remove HTML element (DOM Node) from memory

Read http://perfectionkills.com/understanding-delete/. The delete operator is not for variables (that's why it returns false).

If you want to remove the variable's reference to the DOM node, use

myCanvas = null;

to overwrite the value. Usually you never need to do this, because the garbage collector of JS does all the work for you.


Just assign another value to myCanvas variable (like null) so that no more variables reference the canvas element. Garbage Collection will do the rest.

Of course, there is no guarantee. This assumes that there are no other variables referencing the element as well. Otherwise, if there are other variables, objects etc. that still reference that canvas element, then it's not removed from memory at all. This gets harder to remove if there are closures that contain the references to the element but have no way to dereference.

Tags:

Javascript

Dom