简体   繁体   English

从内存中删除HTML元素(DOM节点)

[英]Remove HTML element (DOM Node) from memory

according to mdn documentation the method removeChild removes a node from the DOM but it still resides in memory. 根据mdn文档 ,方法removeChild从DOM中删除一个节点,但它仍然驻留在内存中。 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... 我尝试过delete操作符,但对象仍然存在...

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

Read http://perfectionkills.com/understanding-delete/ . 阅读http://perfectionkills.com/understanding-delete/ The delete operator is not for variables (that's why it returns false ). 删除操作符不适用于变量(这就是它返回false的原因)。

If you want to remove the variable's reference to the DOM node, use 如果要删除变量对DOM节点的引用,请使用

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. 通常你永远不需要这样做,因为JS的垃圾收集器为你做了所有工作。

Just assign another value to myCanvas variable (like null ) so that no more variables reference the canvas element. 只需为myCanvas变量指定另一个值(如null ),以便不再有变量引用canvas元素。 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. 否则,如果还有其他变量,对象等仍然引用该canvas元素,那么它根本不会从内存中删除。 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. 好吧,如果您在没有var关键字的情况下初始化myCanvas变量,那么您的代码片段就可以工作了,因为这些变量可以自动configurable: true ,因此可以使用delete运算符进行处理而不会有任何麻烦。

Or else, you could have worked with the object reference off the getElementsByClassName() method instead of off the individual HTMLElement itself 或者,您可以使用getElementsByClassName()方法中的对象引用而不是单独的HTMLElement本身

-- I'm assuming here that myCanvas is the result of a getElementById() operation or the like -- - 我在这里假设myCanvasgetElementById()操作之类的结果 -

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. 因为getElementsByClassName()生成的HTMLCollection将自动更新并在从树中删除后立即删除对该DOM对象的任何引用。

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. 基本答案是引用画布的所有变量都需要设置为undefined以便垃圾收集器可以完成其工作。 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: 在尝试完全删除动态创建的HTML canvas元素以避免内存泄漏时,我遇到了几个棘手的情况:

(1) Methods I had added to the canvas element held their own references to it (creating a cycle). (1)我添加到canvas元素的方法保存了它们自己的引用(创建一个循环)。 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. (2)其他对象中的内部变量仍然引用了画布。 Fixed by setting them all to undefined : 通过将它们全部设置为undefined来修复:

_myVar = myBuilder = myObserver = undefined;

(3) A variable referenced the canvas' context: var ctx = myCanvas.getContext('2d'); (3)变量引用画布'context: var ctx = myCanvas.getContext('2d'); This held on to the canvas in some browsers (even after setting myCanvas = undefined ). 这在某些浏览器中保留在画布上(即使在设置myCanvas = undefined )。 Fixed by clearing that variable too: 通过清除该变量来修复:

ctx = undefined;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM