最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove HTML element (DOM Node) from memory - Stack Overflow

programmeradmin2浏览0评论

according to mdn documentation the method removeChild removes a node from the DOM but it still resides in memory. My problem is that I want to delete it from memory as well. I've tried with the delete operator but the object is still there...

myCanvas.parentElement.removeChild(myCanvas);  // myCanvas actually removed from DOM
delete myCanvas;  // false. does nothing
alert(myCanvas); // shows HTMLCanvasElement instead of undefined

according to mdn documentation the method removeChild removes a node from the DOM but it still resides in memory. My problem is that I want to delete it from memory as well. I've tried with the delete operator but the object is still there...

myCanvas.parentElement.removeChild(myCanvas);  // myCanvas actually removed from DOM
delete myCanvas;  // false. does nothing
alert(myCanvas); // shows HTMLCanvasElement instead of undefined
Share Improve this question edited Aug 1, 2012 at 12:16 Florent 12.4k10 gold badges50 silver badges58 bronze badges asked Aug 1, 2012 at 12:13 lviggianilviggiani 6,06612 gold badges60 silver badges93 bronze badges 3
  • Just out of curiosity, can I ask why? – David Thomas Commented Aug 1, 2012 at 12:16
  • 1 Unreference myCanvas from everywhere setting variables to null – Florent Commented Aug 1, 2012 at 12:17
  • 2 Beacuse my js application is dynamically creating and deleting many images (canvas) and I want to make sure that I don't create any memory leaks. – lviggiani Commented Aug 2, 2012 at 12:57
Add a comment  | 

4 Answers 4

Reset to default 11

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.

Well, your code snippet could have worked if you initialized your myCanvas variable without the var keyword since these variables become configurable: true automatically and thus can be disposed of with the delete operator without any hassle.

Or else, you could have worked with the object reference off the getElementsByClassName()method instead of off the individual HTMLElement itself

-- I'm assuming here that myCanvas is the result of a getElementById() operation or the like --

Because the HTMLCollection generated by the getElementsByClassName() will update itself and remove any reference to the DOM object in question as soon as it's removed from the tree.

In other words, it's live by nature, and therefore it doesn't need any manual operation to destruct/purge the references.

The basic answer is that all variables referencing the canvas need to be set to undefined so that the garbage collector can do its job. But sometimes it's not so simple in practice.

Here are several tricky situations I came across when trying to completely remove dynamically-created HTML canvas elements to avoid memory leaks:

(1) Methods I had added to the canvas element held their own references to it (creating a cycle). I fixed this by deleting all custom properties when removing the canvas:

function deleteAllProperties(obj) {
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            delete obj[key];
        }
    }
}
deleteAllProperties(myCanvas);
myCanvas = undefined;

(2) Internal variables in other objects still referenced the canvas. Fixed by setting them all to undefined:

_myVar = myBuilder = myObserver = undefined;

(3) A variable referenced the canvas' context: var ctx = myCanvas.getContext('2d'); This held on to the canvas in some browsers (even after setting myCanvas = undefined). Fixed by clearing that variable too:

ctx = undefined;
发布评论

评论列表(0)

  1. 暂无评论